Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 713
Paul Batum Avatar asked Aug 17 '09 12:08

Paul Batum


People also ask

How do I know my git branch name?

Using the git name-rev Command As the output above shows, the git name-rev command prints the current branch name as well.

How do I show a git branch in Terminal bash?

The git_branch() is a function, that prints the name of the current Git branch in the round brackets. We set the PS1 variable and place the function git_branch() inside it to display the Git branch in the terminal prompt.


2 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.

like image 144
sevenforce Avatar answered Oct 06 '22 08:10

sevenforce


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:

enter image description here

Example 2:

enter image description here

like image 21
tamj0rd2 Avatar answered Oct 06 '22 09:10

tamj0rd2