Git Branch in Powershell Prompt
If you’ve ever used Git Bash before, you’ve likely seen how it shows the current branch name as part of the command prompt. You can get the same functionality in Powershell with a very simple script.
Update your Powershell profile by opening this file:
notepad $PROFILE
Add the following script:
function prompt {
$currentPath = Get-Location
$gitBranch = ''
if (Get-Command git -ErrorAction SilentlyContinue) {
try {
$branch = git rev-parse --abbrev-ref HEAD 2>$null
if ($branch -and $branch -ne 'HEAD') {
$gitBranch = " [$branch]"
}
} catch {
# Not in a Git repo, do nothing
}
}
return "$currentPath$gitBranch> "
}