Show Current Git Branch in PowerShell

If you happen to switch between branches a lot, or find yourself doing git branch to see what branch you are currently on, you can easily modify your PowerShell profile to show your current active branch right in the prompt. Here is how.

Step 1

Open powershell profile for edit by entering “notepad $profile” in powershell. This will open your PowerShell profile located here: C:\Users\yourname\Documents\WindowsPowerShell

Step 2

Add the following function in the profile definition.

function prompt {
$host.ui.rawui.WindowTitle = $(get-location)

$prompt_string = “PS ” + $(get-location) + ” [”

if(Test-Path .git) {
git branch | foreach {
if ($_ -match “^\*(.*)”){
$prompt_string += $matches[1] + ” ]> ”
}
}
}
else{
$prompt_string = “PS> ”
}

Write-Host ($prompt_string) -nonewline -foregroundcolor yellow
return ” ”
}

Step 3

Save and re-open the powershell. Your prompt should now show as following in yellow with branch name showing in the square brackets:

PS C:\Projects\foo [ x.x.currentBranch ]>

Advertisements
Show Current Git Branch in PowerShell

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s