How can I display my current git branch name in my PowerShell prompt?
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:
[{HEAD-name} +A ~B -C !D | +E ~F -G !H]
(For installing posh-git I suggest using psget)
If you don't have psget use the following command:
(new-object Net.WebClient).DownloadString("https://raw.githubusercontent.com/psget/psget/master/GetPsGet.ps1") | iex
To install posh-git use the command:Install-Module posh-git
To ensure posh-git loads for every shell, use the Add-PoshGitToProfile
command.
Here's my take on it. I've edited the colours a bit to make it more readable.
Microsoft.PowerShell_profile.ps1
function Write-BranchName () { try { $branch = git rev-parse --abbrev-ref HEAD if ($branch -eq "HEAD") { # we're probably in detached HEAD state, so print the SHA $branch = git rev-parse --short HEAD Write-Host " ($branch)" -ForegroundColor "red" } else { # we're on an actual branch, so print it Write-Host " ($branch)" -ForegroundColor "blue" } } catch { # we'll end up here if we're in a newly initiated git repo Write-Host " (no branches yet)" -ForegroundColor "yellow" }}function prompt { $base = "PS " $path = "$($executionContext.SessionState.Path.CurrentLocation)" $userPrompt = "$('>' * ($nestedPromptLevel + 1)) " Write-Host "`n$base" -NoNewline if (Test-Path .git) { Write-Host $path -NoNewline -ForegroundColor "green" Write-BranchName } else { # we're not in a repo so don't bother displaying branch name/sha Write-Host $path -ForegroundColor "green" } return $userPrompt}
Example 1:
Example 2:
@Paul-
My PowerShell profile for Git is based off of a script I found here:
http://techblogging.wordpress.com/2008/10/12/displaying-git-branch-on-your-powershell-prompt/
I've modified it a bit to display the directory path and a bit of formatting. It also sets the path to my Git bin location since I use PortableGit.
# General variables$pathToPortableGit = "D:\shared_tools\tools\PortableGit"$scripts = "D:\shared_tools\scripts"# Add Git executables to the mix.[System.Environment]::SetEnvironmentVariable("PATH", $Env:Path + ";" + (Join-Path $pathToPortableGit "\bin") + ";" + $scripts, "Process")# Setup Home so that Git doesn't freak out.[System.Environment]::SetEnvironmentVariable("HOME", (Join-Path $Env:HomeDrive $Env:HomePath), "Process")$Global:CurrentUser = [System.Security.Principal.WindowsIdentity]::GetCurrent()$UserType = "User"$CurrentUser.Groups | foreach { if ($_.value -eq "S-1-5-32-544") { $UserType = "Admin" } }function prompt { # Fun stuff if using the standard PowerShell prompt; not useful for Console2. # This, and the variables above, could be commented out. if($UserType -eq "Admin") { $host.UI.RawUI.WindowTitle = "" + $(get-location) + " : Admin" $host.UI.RawUI.ForegroundColor = "white" } else { $host.ui.rawui.WindowTitle = $(get-location) } Write-Host("") $status_string = "" $symbolicref = git symbolic-ref HEAD if($symbolicref -ne $NULL) { $status_string += "GIT [" + $symbolicref.substring($symbolicref.LastIndexOf("/") +1) + "] " $differences = (git diff-index --name-status HEAD) $git_update_count = [regex]::matches($differences, "M`t").count $git_create_count = [regex]::matches($differences, "A`t").count $git_delete_count = [regex]::matches($differences, "D`t").count $status_string += "c:" + $git_create_count + " u:" + $git_update_count + " d:" + $git_delete_count + " | " } else { $status_string = "PS " } if ($status_string.StartsWith("GIT")) { Write-Host ($status_string + $(get-location) + ">") -nonewline -foregroundcolor yellow } else { Write-Host ($status_string + $(get-location) + ">") -nonewline -foregroundcolor green } return " " }
So far, this has worked really well. While in a repo, the prompt happily looks like:
GIT [master] c:0 u:1 d:0 | J:\Projects\forks\fluent-nhibernate>
*NOTE: Updated with suggestions from Jakub Narębski.
- Removed git branch/git status calls.
- Addressed an issue where 'git config --global' would - fail because $HOME was not set.
- Addressed an issue where browsing to a directory that didn't have the .git directory would cause the formatting to revert to the PS prompt.