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 []

5 thoughts on “Getting posh-git in all your PowerShell consoles using GitHub for Windows”

  1. For larger repositories, the git status call, used to determine individual file status, is too slow to tolerate every time you press Enter. You can either disable it altogether in your $PROFILE by setting:

    $GitPromptSettings.EnableFileStatus = $false
    

    Or you can disable it for specific projects:

    $GitPromptSettings.RepositoriesInWhichToDisableFileStatus += "C:\path\to\project"
    

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.