Day 63: Using GIT for Windows for PowerShell DSC Source Control (part 1)

Welcome to Day 63 of the “100 Days of DevOps with PowerShell”! For background on our goals in this series, see Announcing the “100 Days of Devops with PowerShell” Series here at SCC. If this is the first time you are viewing an article of the series and you are new to DevOps and PowerShell Desired State Configuration (DSC), I encourage you to read through the previous installments, particularly Day 1. I can’t believe we are already in the second half of our 100 days! As we progress, expect us to push further into new concepts, as well we to tie together our work from previous days.

Objective of this post…

Mickey is speaking at TechEd Europe in Barcelona this week, so in his absence I wanted to share some work I did recently to establish source control for my DSC PowerShell snippets I am creating to automate the DSC configuration mof build process, as Scott finished describing in Day 56: Automating the DSC Configuration MOF Build Process (part 3). I am going to show you how to install, configure and check your code into Git, a full-featured but lightweight version control system that can run on your desktop.

In this post:

  • What is Git? (a quick intro for IT Pros)
  • Download Git for Windows
  • Install Git
  • Configure Git
  • Check in Your DSC PowerShell Snippets

I have Git for Windows installed on my Surface Pro 3, and will help you get Git installed, create your first repository and perform your first check-in of your PowerShell DSC source code.

What is Git? (a quick intro for IT Pros)

Git is a distributed revision control system with an emphasis on speed,data integrity,and support for distributed, non-linear workflows. Git was initially designed and developed by Linus Torvalds in 2005, and has since become the most widely adopted version control system for software development. As with most other distributed revision control systems, and unlike most client–server systems, every Git working directory is a full-fledged repository with complete history and full version-tracking capabilities, independent of network access or a central server. Like the Linux kernel, Git is free software distributed under the terms of the GNU General Public License version 2.

Version control software allows you to have “versions” of a project, which show the changes that were made to the code over time, and allows you to backtrack if necessary and undo those changes. This ability alone – of being able to compare two versions or reverse changes, makes it invaluable when working on larger projects. GIT is a distributed revision control system with an emphasis on speed,data integrity,and support for distributed, non-linear workflows. Git was initially designed and developed by Linus Torvalds in 2005, and has since become the most widely adopted version control system for software development.

While centralized version control allows multiple developers to work on a project, it can be problematic in that nobody can work if the system is unavailable. In a distributed version control system like Git, clients don’t just check out the current version of the files and work from them – they mirror the entire version history. Each developer always has a complete copy of everything. A central server is still used, but should the worst happen, then everything can still be restored from any of the clients who have the latest versions.

 

Download GIT for Windows

You can download Git for Windows from the following URL.

http://msysgit.github.io/

The installer is a single .exe file and installation is very simple.

Install GIT

Double-click the .exe file you downloaded to begin the installation. You are basically going to accept all the defaults and installation will be complete in only a minute or two.

SNAGHTML3a526a8

Click Next on each of the following screens to accept the defaults.

SNAGHTML3a57891

 

SNAGHTML3a5b8e5

 

SNAGHTML3a5b8e5[4]

 

SNAGHTML3a631de

The PATH environment decision is important. Excepting the default here ensures installing Git on your computer does not impact your existing PATH statement.

SNAGHTML3a68bc6

Click Next again to start the installation.

SNAGHTML3a71038

This option is also important, as we are installing on Windows, not Linux.

SNAGHTML3a71038[5]

 

SNAGHTML3adec72

Now we are ready to do some basic configuration.

 

Configure Git

I am going to take you through some very quick configuration basics here. I cannot teach you everything about Git in this post, so I’ll ask you to trust me in simply performing the following steps. In the interim between now and my next installment on Git for DSC source control, you can have a look at this free online tutorial on Git: http://www.vogella.com/tutorials/Git/article.html, which will explain the details of my quick configuration steps below.

Create a New Git Repository

Start by creating a new Git repository. Go to the Start screen and launch the Git user interface by typing Git and launching Git GUI

image

Now, click Create New Repository to launch the wizard.

SNAGHTML3be6df8

Enter the path of your choice. In this case, I added the repository in the root of my profile folder.

SNAGHTML3bfc9bf

Let’s close the GUI and switch to the Git Bash shell to complete the configuration

SNAGHTML3c1342d

 

Finish Initial Configuration in the Git Bash Shell

To finish configuration, go back to the Start screen and launch the Git shell by typing Git and launching Git Bash, the Git command line interface. 

image

To open your repository, change directories to the root of the Git repository you just created, but typing the following command, substituting your path where mine is listed.

cd /c/users/Pete/DSCSource

image

User Configuration

Configure your user and email for Git via the following command.

# configure the user which will be used by git
# of course you should use your name
git config --global user.name "Pete Zerger"

# same for the email address
git config --global user.email pete.zerger@mydomain.com 

Push configuration

The following command configure Git so that the git push command pushes only the active branch (in case it is connected to a remote branch, i.e.configured as remote tracking branches) to your Git remote repository. As of Git version 2.0 this is the default and therefore it is good practice to configure this behavior.

# set default so that only the current branch is pushed
git config --global push.default simple

Avoid merge commits for pulling

If you pull in changes from a remote repository, Git by default creates merge commits if you pull in divergent changes. This may not be desired and you can avoid this via the following setting.

# set default so that you avoid unnecessary commits
git config --global branch.autosetuprebase always 

Query Git settings

To query your Git settings of the local repository, execute the following command:

git config --list 

With the basic configuration out of the way, we are ready to add our PowerShell DSC code snippets to the Git repository.  

 

Check in Your DSC PowerShell Snippets

After adding the selected files to the staging area, you can commit these files to permanently add them to the Git repository. Committing creates a new persistent snapshot (called commit or commit object) of the staging area in the Git repository. The staging area keeps track of the snapshots of the files until the staged changes are committed.

For committing the staged changes you will use the git commit command.

image

Check in PowerShell DSC Code Snippets

First, I copied my folder of DSC code snippets called \ConfigurationTemplates. into the root of my Git repository (c:\users\Pete\DSCSource).

Now, run the following command to the current status of your repository.

git status

 

image_thumb[1]

 

Add files to the staging area

Before committing changes to a Git repository you need to mark those that should be committed. This is done by adding the new and changed files to the staging area by running the command below. This creates a snapshot of the affected files. Make sure you type the space and period(.).

git add .

now check status again

git status 

image_thumb[3]

Commit to Git repository

After adding the files to the Git staging area, you can commit them to the Git repository. This creates a new commit object with the staged changes in the Git repository and the HEAD reference points to the new commit. The -m parameter allows you to specify the commit message. If you leave this parameter out, your default editor is started and you can enter the message in the editor.

# commit your file to the local repository
git commit -m "Initial commit" 

image_thumb[5]

Looking at the result

The Git operations you performed have created a local Git repository in the .git folder and added all files to this repository via one commit. Run the git log command

# show the Git log for the change
git log 

image_thumb[7]

 

See all commits of a certain user

You can use the --author tag to filter the log output by a certain author. You do not need to use the full name, if a substring matches, the commit is included in the log output.

The following command lists all commits with an author named “Pete Zerger”.

git log --author=”Pete Zerger”

image_thumb[9]

That’s it. Your initial Git installation, repository setup and first code check-in are complete. When we revisit Git later in the series, we will look at retrieving code snippets from the repository to build on the automated DSC configuration mof build process demonstrated in Day 56.

 

Conclusion

That’s all for this installment. I hope you were able to setup your Git repository so you have a lightweight version control repository.

Previous Installments

To see the previous installments in this series, visit “100 Days of DevOps with PowerShell”.

Leave a Reply