jimmy keen

on .NET, C# and unit testing

Using Git with Powershell on Windows

November 10, 2015 | tags: git windows powershell posh-git

This will be a quick guide on how to set up Powershell with Git on Windows.

Essentials

  1. Powershell
  2. Git for Windows
  3. Git credentials store

Download and install files above. Make sure Git bin directory is added to your PATH environmental variable. Credentials store are not really required but I’m pretty sure you do not want to type your login details with every push or pull, right?

Aliases

Just as with any console application, Git offers a way to streamline your work with aliases. Rather than typing

> git add -A

> git commit -m "Added config file"

You can simply type

> git c "Added config file"

This of course goes further than that considering we have Powershell at our disposal. For example, you can easily create alias like this:

> git config --global alias.logby "!f() { git log --author='`$1'; }; f"

> git logby Jimmy

We create f function and execute it straight away. You can read more about this approach at “Git alias with parameters”. When setting such alias from powershell itself, make sure to escape $-variables with ` (grave accent symbol), otherwise powershell will try to insert variables which are not there.

You can see some more aliases here.

The Posh

A neat little extension offering git commands completion and making your Powershell look like this:

posh-git powershell highlighting

Posh-git can be installed from powershell:

> (new-object Net.WebClient).DownloadString("http://psget.net/GetPsGet.ps1") | iex

> Install-Module posh-git

If your powershell compains that your execution policy is restricted, make sure to un-restrict it with:

> Set-ExecutionPolicy RemoteSigned

It’s great we can see current branch name. It’s also great that we can see whether there were any modifications. But I’m not a huge fan of having all the details of changed files, untracked, stashed and so forth. I would rather have this:

posh-git powershell simplified highlighting

A simple indicator telling me what branch I am on and what’s the status (* - pending changes, - synced with origin, - ahead of origin).

Behavior described above can be achieved if we modify posh-git code, namely GitPrompt.ps1 file. You can find this file location in one of your powershell modules directories (check $env:PSModulePath variable). You can use the files from my repository:

And that’s it. Enjoy your brand new Git + Powershell experience!