PowerShell git: The beginning! I am writing this blog post from the airport / air plane heading back to Basel. I was at the Powershell summit (#pshsummit) in Stockholm and I really had a GREAT time!

Not only I could see my old #powershell friends, but I got the occasion to meet a lot more! The feeling of beeing part of a community grows each time I have an interaction with anybody who does something related to #powershell over the internet. But is multiplied by 400 when you meet these same people physically! It is just great!

The sessions were great, and I will be blogging about a few a them for sure in the near future, but one topic that I could actually get covered here in the airplane, was how to use version control with powershell. With some of Microsoft’s code beeing open sourced, there has been a real shift in Windows world towards version control.

The topic has been covered in the following sessions:

  • Microsoft PowerShell team –> session title: Community + Powershell -eq ‘Achieve more’
  • Simon Wahlin  (@SimonWahlin) –> Session title: I am not a developper.Or am I?

While the first one was more on how the Microsoft PowerShell team now works on in an open source world, they explained us how we could make feature requests, file bugs, and even troubleshoot our own ones, and send pull request once we have corrected any potential bugs.

The second session present by Simon was really a session from learn git in an hour. It was really great, and I really learned a lot. I actually learned so much, that it got me totaly hyped up. I immediatly downloaded the module (see below on how to do that) and started to work with it.


There is to much blabla for you and want to get straight to the learning point? Scroll down to part 1, for the basics. Or go directly to the bottom at step 10 to have a nice overview in a mind map format on how to start to use powershell git (posh-git) for Windows.


 

If you write a lot of code, and if you are NOT using any software to organize your changes in your code, you are loosing time and version control will change your life!

Some people might say “Yeah, I am not a developper” or “I dont want to do that, I am just a scripter”. But as Simon mentionned: If you are writing code that is used in production, you must take responsibility like a developper.

Twitter

 

And to complete with a quotation from  Steve Murawski (@steveMurawski) “If you write code every day, you are developer: Get over it!”

I would like to add and complete, that times have changed. A few years back there was this kind of strange competition between developers and IT Pros. Each of us would make silly jokes about the the other ones.

But today the line that separated our daily tasks has become so thin, close to unexisting, that that competetion is useless, and looks like and old image in black and white from the past. Today, we talk about Devops, which is how we join our forces to make us biger and stronger. One of the bridges that is used by IT pros to link to the developers, is without any doubt “PowerShell”

No matter what language we use to write code. If it is a scripting language or a lower level programming language: If code is used in production, having it stored in simple folders, is not enough anymore.

Why should I care about version control?

You can deliberatly choose not to care. I think that as long as everything works fine, nobody will force you to change your habits. But, when you loose code that was in production, and that you loose hours of work, or if you break something in production by updating a script, and cannot roll back (or dont know how), that’s the moment when you will hate yourself for not having invested time in updating your skill set, and adding version control with GIT for powershell in it.

The learning curve to get you started will be small, and easy. It is afterwards, during your code writing process, that you will need to change a few of your habits. It is time to get structured!

If you are decided to get the powershell git knowledge, keep reading!

0) what are the powershell git / posh-git prerequisites ?

Ok great, you are still here!

Setting up the prerequisites, will take you 5 minutes. Simply follow the 5 steps I have highlighted for you in the mind map below, and everything should roll smoothly!

posh git mind map prerequisites

Each powershell git prerequisit step is detailed here under:

a) Download and install git.

You can download git directly from the official website here.

You can also download and “Github desktop“, which comes with a posh powershell prompt called ‘Git Shell’. Which you will need to use if you want to have the git coloring syntax in your prompt.”

b) Install posh-git

 

To start, you will want to get the binaries to manage git localy on your authoring machine. PowerShell 5.0 made that super easy for us. Open a command prompt with elevated priviledges and run the following command;

If you don’t have the cmdlet “Find-Module”, then you are not using PowerShell 5.0 yet. This is ok, but you will need to download Posh-git manually then. You can get it directly from the official git-scm website here.

c) Update your path environment variable

You might have an error message saying you don’t have the %programfiles%gitcmd in your %path% variable.

This might not be necessary for you, but on my machine (Windows 8.1) with Powershell version 5.0 production preview from August, I needed to update the content of my path variable otherwise the git commands refused to work. For that I simply added the following line to my %path% environment

Either add it graphically using Windows + Pause Advanced EnvironmentVariables System Variable

And the following line at the end of your %path% variable (using “;” to separate it from the last line).

%ProgramFiles%gitcmd

Or directly using powershell (you will need to close and reopen your prompt to make the changes take effect) by using the following command:

d) Update your profile script:

The easiest way to do that is launch the install.ps1 that is located in the following path : C:Program FilesWindowsPowerShellModulesposh-git<versionNumber>Install.ps1

Or you can do it manually by following the profile script example located here:C:Program FilesWindowsPowerShellModulesposh-git<versionNumber>profile.example.ps1

(The content of the profile.example.ps1 is actually added to your profile script when you call the install.ps1 file.

Thats about it concerning the prerequisites. You are all set to go!

e) How can I change my contact information (non mandatory):

Before you begin, I recommend that you change your contact information that is hold in GIT. Foreach commit, the name and email information of the author of the code change will be saved together with the changed code. This is a good thing since it will help to keep track who did the changes and why. There is a default value of LoggedinUsername@computername.com but it makes more sense to put accurate information that will allow your coworkers to identify you easily and get in touch with you quickly. To change this Simply write the two following lines of code in a git command prompt.

How to get started with version control using powershell git / posh-git for windows

once you have installed the prerequisites, you can follow the mind map below that will walk you through each time you want to add a new repository into a version control process. I detail each one of the 6 steps below.

mind map posh git basics

 

Step 1: Create your Master Branch

Creating your master branch basically means “enabling git” in your working directory.

First, create a directory that will be the name of your project.

In my case, I called it “GitDistrict”


powershell git init

 

If we go have a look in our “GitDistrict” folder now, we will find a hidden folder called “.git” which will contain all the information concerning our version control.

powershell git

From this point on, version control is ‘on’. You can start to create documents in your folder and write your first drafts.

Step 2: include files into your git repository

In my demo folder I have copied the latest version of my BitlockerSAK script. (If you have to achieve bitlocker automation tasks I recommend you have a look at my Bitlocker Swiss Army Knife project here).

If we look at what the folder contains using the good old “dir” command we will a bunch of new colored numbers next to the word “Master”:

02_New file added but not yet in git repository

 

Wait wait wait! Were did all these numbers came from? what do they mean? well, there is nothing like an image to explain thousands words:

git posh colors

 

Basically, each time you see information with the color ‘red’ in there, that means that something is ‘pending’. I will go through all the cases in this blog post, so keep reading 🙂

The file is detected by git but it is not taken into consideration by the version control mechanism.

This means that adding files only directly into your working directory will not be enough to integrate them into your version control system. You will have to explicitly add them.

There is nothing more easy then that with powershell git:

You will notice that the color will switch to green, which means that your file is currently present in your working directory.

powershell git add

From this point on all modifications to this file will be noticed and logged. This process is called ‘staged‘ . The modification here (Which is the addition of one new file) has been added to the staging area. Basically, as long as we have numbers after the ‘master’ section (or what ever the name of our branch is) you will need to commit these changes first.

Step 3: Committing your changes using powershell git:

Now that the file has been set in the staging area, we have to commit the process in order to make this let’s say: ‘official’.

To commit our changes, we will use the following command:

git commit -m “Your commit message”

 

powershell git commit

In this particular case, we can consider that the file BitlockerSAK.ps1 which is in it’s version 1.6 (In my old way of using ‘version control’) is the start point. From now on, all the changes can be reverted to previous changes, and ultimately to this very first version of 1.6.

Your curious on how we can revert back to a previous version, or save more recent and updates script states? Continue reading, we are almost there! 😉

Step 4:  saving changes made to our file (after some work is done):

Now that we have our file in our file system, and monitored by git, we can go ahead and work on our file. Everything we do will be logged and monitored.

To start slowly, I  simply add one fake comment in the BitlockerSAK script.

powershell git test file modification

 

If we go back to our powershell prompt, and take a look what our folder contains now, we will see that the changes are automatically detected. The tilt 1 in red indicates that change as illustrated below.

newly modified file

Notice how the numbers are all in red. This actually means that the changes have been detected, but not taken into consideration yet. Or better said: The changes are not yet added into the staging area.

In order to add these changes into our staging area, we will use the same command as when we added the file at the very beginning with powershell git:

05 git add filename

 

once we have added our changes to the staging area, notice how the numbers went from “red” meaning not staged yet, to “green“, which means, all files have been staged.

There is a good and easy way to see all the changes that you are going to commit using powershell git. Bear with me, because I’ll show you that in the next example.

Step 5: Committing your changes with powershell git

Once you are finished with your modifications, you can commit the code 
as follows:

Visually, it will look like this in your prompt:

powershell Git Commit -m

 

 

Step 6: get a global status of your repository:

 

In order to get all the information’s of your repository, use the following command from powershell git:

It will then return all the pending changes, pending deletions and additions from your ‘Branch’ (read your repository). In my example, and in this current state, there is nothing pending as demonstrated here under.

 

git status

Isn’t there an easier way to see this?

Yes there is. Keep on reading 😉

Step 7: How to have a global vision of your git repository?

 

Now that we have saved our first file and commited our changes, we might want to have a global overview how what exactly happended through time on our bitlockersak.ps1 file.

in your same powershell prompt, type the following line of powershell git:

You will then have a nice little surprise: A graphical interface! 🙂

powershell git gitk

 

gitk is really great! It allows us to have a graphical vision on what has happend on our file latley. The color schema is pretty straightfoward: Red minus is when something has been removed. (In my example, I removed a white empty space). The green plus means that that line has been added (compared to the prior version).

Step 8: Summary and powershell git (posh-git) mind map

To summarize this introduction to git, you will find a mind map with the all the steps that we have been through, and that will help you to get started with version control.

git Mind map global

Green, red yellow. What is it about all these colors in Posh-git (powershell git) you may ask? Here’s a short mind map containing the basic information about the colors.

git posh colors mind map

 

If you are interested in having a copy of the mind maps, please let me know and I will make them available online.

That’s about it for part 1 of this blog post. In part two I will cover how to connect and work on github, which is helpful since Microsoft open sourced there DSC resources there.

Digg more into it!

You want to digg more into it, have a look at these excellent post / videos:

  • Great crash course video from Warren Frame on techsession –> Check it out here