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:
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+.
- (Optional) First, install PowerShell Core (version 6+) so you’ll have the latest and greatest at time of writing.
- Open PowerShell Core
Get-ExecutionPolicy
- This should be
RemoteSigned
orUnrestricted
. If it’s not, you might need to run the following from an elevated prompt:Set-ExecutionPolicy RemoteSigned -Scope CurrentUser -Confirm
- This should be
git --version
and you should see output indicating PowerShell can access git from your path.- 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
- If you have installed it before, try this:
- Tell PowerShell you want to use it whenever you’re in a folder with a .git subfolder:
Import-Module posh-git
- Tell PowerShell you want to use this module whenever you open PowerShell:
Add-PoshGitToProfile
- 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:
- What branch you have checked out
- Whether there are differences
- Whether files are staged
- 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:
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!