Getting posh-git in all your PowerShell consoles using GitHub for Windows

If you use git for version control and you use Microsoft Windows, you may well have used posh-git, a module for PowerShell. For those that have not, posh-git adds some git superpowers to your PowerShell console including tab completion for git commands, files and repositories, as well as an enhanced command prompt that tells you the current branch and its state1.

PowerShell console using posh-git
PowerShell console using posh-git
GitHub for Windows
GitHub for Windows

GitHub for Windows includes posh-git for its PowerShell console, if you choose that console when installing or later in the settings. It even adds a nice console icon to the task bar and Start screen2. Unfortunately, posh-git is only installed for the special version of the console that GitHub for Windows provides and you cannot make that prompt run as administrator, which can be useful once in a while.

Now, you could install a separate version of posh-git for all your other PowerShell needs, but that seems wrong. Especially since GitHub for Windows will happily keep its version up-to-date but you'd have to keep track of your other installation yourself.

Faced with this problem, I decided to hunt down how GitHub for Windows installed posh-git to see if I could get it into the rest of my PowerShell consoles. I quickly discovered ~\AppData\Local\GitHub containing both the posh-git folder and shell.ps1, the script that sets up the GitHub shell. The fantastic part of this script is that it sets up an environment variable for posh-git, github_posh_git, so you don't even need to worry about whether the folder changes3.

Armed with this information, you can edit your PowerShell profile4 and edit it to call both the GitHub shell script and the example profile script for posh-git5.

# Load posh-git example profile
. (Resolve-Path "$env:LOCALAPPDATA\GitHub\shell.ps1")
. (Resolve-Path "$env:github_posh_git\profile.example.ps1")

cd ~/Documents/GitHub

Once the edits are saved, close and reopen the PowerShell console to run the updated profile. Posh-git should now be available and all you have to do to keep it up-to-date is run the GitHub for Windows client once in a while.

  1. such as if there are any unstaged or uncommitted files and whether the branch is behind, ahead, or diverged from the remote []
  2. or menu, if you're pre-Windows 8 or installed something to bring the Start menu back []
  3. and if you've seen the folder name for posh-git in the GitHub for Windows installation, you'll see why that's useful []
  4. just enter `notepad $profile` at the PowerShell prompt []
  5. you may want to do the same thing for the PowerShell ISE, as it uses a separate profile script []

Git integration for all your PowerShells with Github for Windows

We use git for our source control at work. In fact, we use Github. I have GitHub for Windows (GfW) installed because it's one of the easiest ways to install git on a Windows desktop. As part of the installation, you get to choose how the git shell is provided; I selected PowerShell (PS). This works well. You can access the integrated console via a separate shortcut or by the ~ key when viewing a repository in GfW.

However, the git integration (provided by posh-git) isn't available in the standard PS console nor PS ISE1. I use PS ISE a lot more these days as it gives me tabbed console windows and some cool features like auto-complete dropdowns2, so I wanted git integration there too.

As I already have git and posh-git installed via GfW, I didn't want to install both separately again just to get this support, I wanted to use what was already there.

To do this, open your PS or PS ISE console (you'll need to do this for both as they have separate profiles) and enter:

notepad $profile

Then add the following lines and save:

# Load github shell and posh-git example profile
. (Resolve-Path "$env:LOCALAPPDATA\GitHub\shell.ps1")
. (Resolve-Path "$env:github_posh_git\profile.example.ps1")

To see the changes, you need to restart your console. If you're sure there'll be no nasty side-effects from running your profile twice in one session, you could also just enter:

. $profile

And there you have it, git support using the GitHub for Windows installation in all your PowerShell windows.

  1. Integrated Scripting Environment []
  2. There are some caveats to using the ISE console tab over a regular PS console []