An easier way would be just installing the Powershell module posh-git. It comes out of the box with the desired prompt:
The Prompt
PowerShell generates its prompt by executing a prompt function, if one exists. posh-git defines such a function in profile.example.ps1 that outputs the current working directory followed by an abbreviated git status:
C:\Users\Keith [master]>
By default, the status summary has the following format:
I tweaked the prompt code (from @david-longnecker answer) to be a bit more colorful.
Edit: June 2019 - Updated to show untracked, stashes, renames. Tweaked visual to show index.
Why I use this (over posh-git etc):
Learning: Learn stuff about git/SCM concepts when as I tweak
End up fixing problems solved by posh-git, but I learn more through this
Cross Platform: Have same prompt in POSIX shells, code is almost identical.
Flexible: I've tweaked my Prompt to show untracked/stashes/index/renames.
Lightweight & Portable: No fetching external modules (this is minor benefit, but nice)
The powershell code:
NOTE: Some commands used are porcelain (not recommended for scripting/parsing, e.g. git status). Will eventually migrate to plumbing commands, but this works for now.
Function Prompt {
$SYMBOL_GIT_BRANCH='⑂'
$SYMBOL_GIT_MODIFIED='*'
$SYMBOL_GIT_PUSH='↑'
$SYMBOL_GIT_PULL='↓'
if (git rev-parse --git-dir 2> $null) {
$symbolicref = $(git symbolic-ref --short HEAD 2>$NULL)
if ($symbolicref) {#For branches append symbol
$branch = $symbolicref.substring($symbolicref.LastIndexOf("/") +1)
$branchText=$SYMBOL_GIT_BRANCH + ' ' + $branch
} else {#otherwise use tag/SHA
$symbolicref=$(git describe --tags --always 2>$NULL)
$branch=$symbolicref
$branchText=$symbolicref
}
} else {$symbolicref = $NULL}
if ($symbolicref -ne $NULL) {
# Tweak:
# When WSL and Powershell terminals concurrently viewing same repo
# Stops from showing CRLF/LF differences as updates
git status > $NULL
#Do git fetch if no changes in last 10 minutes
# Last Reflog: Last time upstream was updated
# Last Fetch: Last time fetch/pull was ATTEMPTED
# Between the two can identify when last updated or attempted a fetch.
$MaxFetchSeconds = 600
$upstream = $(git rev-parse --abbrev-ref "@{upstream}")
$lastreflog = $(git reflog show --date=iso $upstream -n1)
if ($lastreflog -eq $NULL) {
$lastreflog = (Get-Date).AddSeconds(-$MaxFetchSeconds)
}
else {
$lastreflog = [datetime]$($lastreflog | %{ [Regex]::Matches($_, "{(.*)}") }).groups[1].Value
}
$gitdir = $(git rev-parse --git-dir)
$TimeSinceReflog = (New-TimeSpan -Start $lastreflog).TotalSeconds
if (Test-Path $gitdir/FETCH_HEAD) {
$lastfetch = (Get-Item $gitdir/FETCH_HEAD).LastWriteTime
$TimeSinceFetch = (New-TimeSpan -Start $lastfetch).TotalSeconds
} else {
$TimeSinceFetch = $MaxFetchSeconds + 1
}
#Write-Host "Time since last reflog: $TimeSinceReflog"
#Write-Host "Time since last fetch: $TimeSinceFetch"
if (($TimeSinceReflog -gt $MaxFetchSeconds) -AND ($TimeSinceFetch -gt $MaxFetchSeconds)) {
git fetch --all | Out-Null
}
#Identify stashes
$stashes = $(git stash list 2>$NULL)
if ($stashes -ne $NULL) {
$git_stashes_count=($stashes | Measure-Object -Line).Lines
}
else {$git_stashes_count=0}
#Identify how many commits ahead and behind we are
#by reading first two lines of `git status`
#Identify how many untracked files (matching `?? `)
$marks=$NULL
(git status --porcelain --branch 2>$NULL) | ForEach-Object {
If ($_ -match '^##') {
If ($_ -match 'ahead\ ([0-9]+)') {$git_ahead_count=[int]$Matches[1]}
If ($_ -match 'behind\ ([0-9]+)') {$git_behind_count=[int]$Matches[1]}
}
#Identify Added/UnTracked files
elseIf ($_ -match '^A\s\s') {
$git_index_added_count++
}
elseIf ($_ -match '^\?\?\ ') {
$git_untracked_count++
}
#Identify Modified files
elseIf ($_ -match '^MM\s') {
$git_index_modified_count++
$git_modified_count++
}
elseIf ($_ -match '^M\s\s') {
$git_index_modified_count++
}
elseIf ($_ -match '^\sM\s') {
$git_modified_count++
}
#Identify Renamed files
elseIf ($_ -match '^R\s\s') {
$git_index_renamed_count++
}
#Identify Deleted files
elseIf ($_ -match '^D\s\s') {
$git_index_deleted_count++
}
elseIf ($_ -match '^\sD\s') {
$git_deleted_count++
}
}
$branchText+="$marks"
}
if (test-path variable:/PSDebugContext) {
Write-Host '[DBG]: ' -nonewline -foregroundcolor Yellow
}
Write-Host "PS " -nonewline -foregroundcolor White
Write-Host $($executionContext.SessionState.Path.CurrentLocation) -nonewline -foregroundcolor White
if ($symbolicref -ne $NULL) {
Write-Host (" [ ") -nonewline -foregroundcolor Magenta
#Output the branch in prettier colors
If ($branch -eq "master") {
Write-Host ($branchText) -nonewline -foregroundcolor White
}
else {Write-Host $branchText -nonewline -foregroundcolor Red}
#Output commits ahead/behind, in pretty colors
If ($git_ahead_count -gt 0) {
Write-Host (" $SYMBOL_GIT_PUSH") -nonewline -foregroundcolor White
Write-Host ($git_ahead_count) -nonewline -foregroundcolor Green
}
If ($git_behind_count -gt 0) {
Write-Host (" $SYMBOL_GIT_PULL") -nonewline -foregroundcolor White
Write-Host ($git_behind_count) -nonewline -foregroundcolor Yellow
}
#Output staged changes count, if any, in pretty colors
If ($git_index_added_count -gt 0) {
Write-Host (" Ai:") -nonewline -foregroundcolor White
Write-Host ($git_index_added_count) -nonewline -foregroundcolor Green
}
If ($git_index_renamed_count -gt 0) {
Write-Host (" Ri:") -nonewline -foregroundcolor White
Write-Host ($git_index_renamed_count) -nonewline -foregroundcolor DarkGreen
}
If ($git_index_modified_count -gt 0) {
Write-Host (" Mi:") -nonewline -foregroundcolor White
Write-Host ($git_index_modified_count) -nonewline -foregroundcolor Yellow
}
If ($git_index_deleted_count -gt 0) {
Write-Host (" Di:") -nonewline -foregroundcolor White
Write-Host ($git_index_deleted_count) -nonewline -foregroundcolor Red
}
#Output unstaged changes count, if any, in pretty colors
If (($git_index_added_count) -OR ($git_index_modified_count) -OR ($git_index_deleted_count)) {
If (($git_modified_count -gt 0) -OR ($git_deleted_count -gt 0)) {
Write-Host (" |") -nonewline -foregroundcolor White
}
}
If ($git_modified_count -gt 0) {
Write-Host (" M:") -nonewline -foregroundcolor White
Write-Host ($git_modified_count) -nonewline -foregroundcolor Yellow
}
If ($git_deleted_count -gt 0) {
Write-Host (" D:") -nonewline -foregroundcolor White
Write-Host ($git_deleted_count) -nonewline -foregroundcolor Red
}
If (($git_untracked_count -gt 0) -OR ($git_stashes_count -gt 0)) {
Write-Host (" |") -nonewline -foregroundcolor White
}
If ($git_untracked_count -gt 0) {
Write-Host (" untracked:") -nonewline -foregroundcolor White
Write-Host ($git_untracked_count) -nonewline -foregroundcolor Red
}
If ($git_stashes_count -gt 0) {
Write-Host (" stashes:") -nonewline -foregroundcolor White
Write-Host ($git_stashes_count) -nonewline -foregroundcolor Yellow
}
Write-Host (" ]") -nonewline -foregroundcolor Magenta
}
$(Write-Host $('>' * ($nestedPromptLevel + 1)) -nonewline -foregroundcolor White)
return " "}#Powershell requires a return, otherwise defaults to factory prompt
The result (VSCode, Using Powershell terminal):
Here are commands from result to view what it would look like:
mkdir "c:\git\newrepo" | Out-Null
cd "c:\git\newrepo"
git init
"test" >> ".gitignore"
"test" >> ".gitignore2"
git add -A
git commit -m "test commit" | Out-Null
"test" >> ".gitignore1"
git add -A
"test1" >> ".gitignore2"
git rm .gitignore
git add -A
git commit -m "test commit2" | Out-Null
git checkout -b "newfeature1"
"test" >> ".test"
mv .gitignore1 .gitignore3
git add -A
git stash
git checkout "master"
cd c:\git\test #Just a sample repo had that was ahead 1 commit
#Remove-Item "c:\git\newrepo" -Recurse -Force #Cleanup
I like the accepted answer so I will elaborate on the steps to set it up.
You can install PoshGit using Chocolatey or using the PowerShellGet command which is available for the new Core PowerShell.
For Chocolatey, you need to have it already installed before proceeding.
In an administrator/elevated shell execute the command:
choco install poshgit
For Core PowerShell, installation is also required. To install Core PowerShell, execute the following command:
dotnet tool install --global PowerShell
NB: You need to have .NET Core SDK installed (latest version preferably)
After installing Core PowerShell, execute the command below to install PoshGit:
Using PoshGit requires you import it to the currently running shell environment using the command Import-Module posh-git. This means you have to run this command every time you open a new shell.
If you want PoshGit to be always available you should execute this command instead:
Add-PoshGitToProfile
Please note that PowerShell and Core PowerShell are different and as a result they run on different profiles. What this means is that if you want PoshGit to work for either of the shells you need to execute those commands in their respective shell environment.