How can I display my current git branch name in my PowerShell prompt?

Viewed 55455

Basically I'm after this but for PowerShell instead of bash.

I use git on windows through PowerShell. If possible, I'd like my current branch name to displayed as part of the command prompt.

9 Answers

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.

With Git 2.22 (Q2 2019), any script (Powershell or not) could use the new --show-current option.

$branch = git branch --show-current

If empty, it means "detached HEAD".

posh-git is slow, there's a better way with https://ohmyposh.dev/.

enter image description here

  1. Run this command from powershell to install ohmyposh module:
Install-Module oh-my-posh -Scope CurrentUser -AllowPrerelease
  1. install font that supports glyphs (icons) from https://www.nerdfonts.com/.
    I like Meslo LGM NF.

  2. set that font in powershell defaults settings:

enter image description here

  1. Open/create file Microsoft.PowerShell_profile.ps1 at C:\Program Files\PowerShell\7 and write below to set theme (same as screenshot):
Set-PoshPrompt -Theme aliens

You can choose other theme also. see preview by running Get-PoshThemes

Now open powershell at location containing git repo and you'll see the status.


See more: Power up your PowerShell

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:

PowerShellGet\Install-Module posh-git -Scope CurrentUser -AllowPrerelease -Force

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.

From @tamj0rd2's answer we can get the branch name to a string variable like this.

$branch = git rev-parse --abbrev-ref HEAD
echo $branch

Here is my configuration for PowerShell Core. just copy the function below and put it in your $PROFILE

function prompt {
  try {
    $GitBranch = git rev-parse --abbrev-ref HEAD
    # we're probably in detached HEAD state, so print the SHA
    if ($GitBranch -eq "HEAD") { $GitBranch = git rev-parse --short HEAD }
  } catch {}

  if ($GitBranch) { $GitBranch = " `e[33;93m[`e[33;96m$GitBranch`e[33;93m]`e[0m" }

  "PS $pwd$GitBranch> "
}
Related