Using posh-git in PowerShell

git logo

Using git on the command line and forgot the status of your repo? PowerShell has a dope plugin called posh-git. It gives you sweet command prompts like:

There’s a bit going on here, but check out that [master] in the prompt

You can reference the installation notes in the repo–I intend to give you the quick & dirty version for faster reference.

The Setup

Assumptions: you have git installed on your platform and can run git at one shell prompt or another (command prompt, older PowerShell, etc) in addition to PowerShell installed. Version doesn’t matter, but the first optional step below suggests installing PowerShell Core 6+.

  1. (Optional) First, install PowerShell Core (version 6+) so you’ll have the latest and greatest at time of writing.
  2. Open PowerShell Core
  3. Get-ExecutionPolicy
    • This should be RemoteSigned or Unrestricted. If it’s not, you might need to run the following from an elevated prompt:
      Set-ExecutionPolicy RemoteSigned -Scope CurrentUser -Confirm
  4. git --version and you should see output indicating PowerShell can access git from your path.
  5. If you’ve never installed posh-git before (probably most of you?) then run:
    PowerShellGet\Install-Module posh-git -Scope CurrentUser -AllowPrerelease -Force
    • If you have installed it before, try this:
      PowerShellGet\Update-Module posh-git
  6. Tell PowerShell you want to use it whenever you’re in a folder with a .git subfolder:
    Import-Module posh-git
  7. Tell PowerShell you want to use this module whenever you open PowerShell:
    Add-PoshGitToProfile
  8. Party

If you ran the commands in the screenshot at the beginning of the article, notice the posh-git prompt changed. It’s trying to tell us, “you have a dirty working directory” or unstaged files. The last icon ! is the indicator for unstaged files.

#6 above is the icing on the cake. Whenever you change directory into a path with a git repo, the git index refreshes (regenerates?) and your prompt gives you several hints about the state of the repository:

  1. What branch you have checked out
  2. Whether there are differences
  3. Whether files are staged
  4. Even whether you’re in a merge state

The Module in Action!

Let’s look at the fresh git repo we started to create above:

Several things happened here…

From a completely empty repo, we added a file called file.txt. Its content is irrelevant, since we just created it and it’s untracked in git. So, git status reveals an untracked file, while the prompt already showed us +1 ~0 -0 ! which translates to:

  • One addition (the untracked file)
  • Zero modifications
  • Zero removed files
  • Dirty working directory (unstaged changes)

After a git add ., the prompt turns green and indicates we’ve added a file and that there are uncommitted changes. From here, you probably know how to git commit -m "commit message" and push your code.

Summary

The git status summary information in the posh-git README gives you more detail on translation of possibilities within the prompt syntax, though the example in this article should give you an idea of what this PowerShell module is capable of. Happy git’ing!

Leave a Reply