= PowerShell and Bash Prompt with Git Branch = **Summary**: This wiki page shows how I configure my PowerShell and bash prompts to include my current git branch and path. \\ **Date**: 1 March 2025 \\ {{tag>powershell bash git}} I work a lot from the command line, both in PowerShell and in Bash. I also work a lot with git repositories, and I really don't like it to constantly check what branch I'm in, or to not be able to read my commands I'm typing because I'm so deep into a directory. To fix that, I created my own custom prompt, for both PowerShell and Bash, that shows me (at least): * if inside a git repository * show current git branch * show only the last 2 parts of the path, but like this: C:\...\part1\part2 [{{prompt-with-git.png?350|My prompt}}] \\ == How does the PowerShell Prompt work? == There is a lot to the PowerShell prompt, so for completeness, [[https://learn.microsoft.com/en-us/PowerShell/module/microsoft.PowerShell.core/about/about_prompts | here is the documentation]]. For this post, all we need to know is that the prompt is defined by a function called prompt, that returns a string. The string is what is shown in the prompt, and to change it, we can just create our own prompt function, which we then need to add to our PowerShell profile. \\ There are two PowerShell profiles I usually use: * Normal PS profile: $PSHOME\Microsoft.PowerShell_profile.ps1 * VS Code PS profile: $HOME\Documents\PowerShell\Microsoft.VSCode_profile.ps1 === PowerShell Prompt === This is the PowerShell prompt I use. It shows the current path (shortened if needed) in green, and if inside a git repository, it shows the current branch in blue. If in a detached HEAD state, it shows the commit sha in red. If the repository is new (no commits yet), it shows [init] in yellow. function Write-BranchName () { try { $branch = git branch --show-current if ([string]::IsNullOrEmpty($branch)) { # Detached HEAD - Print the commit sha $branch = git rev-parse --short HEAD Write-Host "[$branch]" -NoNewline -ForegroundColor "red" } else { # In a branch Write-Host "[$branch]" -NoNewline -ForegroundColor "blue" } } catch { # New repo Write-Host "[init]" -NoNewline -ForegroundColor "yellow" } } function prompt { $base = "PS " $path = "$($executionContext.SessionState.Path.CurrentLocation)".Split("\") if ($path.Count -gt 3){ $path = $path[0] + "\...\" + $path[-2] + "\" + $path[-1] } else { $path = $path -join "\" } $userPrompt = "$('>') " Write-Host "`n$base" -NoNewline if (git rev-parse --show-toplevel 2> $null) { Write-Host $path -NoNewline -ForegroundColor "green" Write-BranchName } else { Write-Host $path -NoNewline -ForegroundColor "green" } return $userPrompt } === Set the profile === Then we need to update our PowerShell profile to include the prompt function above. To do that, we can use the following commands: # Show the profile path echo $PROFILE # Open the profile in VS Code code $PROFILE # paste code above and save the file > You need to repeat that for all of your PowerShell profiles, and restart PowerShell to see the changes. == Bash == === Bash Prompt (in WSL) === The method for the bash prompt in WSL also works for Bash on regular Linux systems. For bash in WSL we will use the official [git-prompt.sh](https://github.com/git/git/blob/master/contrib/completion/git-prompt.sh) script, created and maintained by the git team. * Download the script and place it in your home directory * {{{wget https://raw.githubusercontent.com/git/git/master/contrib/completion/git-prompt.sh}}} * Rename the script like this {{{mv git-prompt.sh .git-prompt.sh}}} Open your bash prompt with {{{vi .bashrc}}} and add the following lines to the file. It doesn't really matter, I like to add them in the section before the prompt (PS1). # Use the git-prompt script to allow for git information in the prompt # https://github.com/git/git/blob/master/contrib/completion/git-prompt.sh . ~/.git-prompt.sh Then, in the same file, locate the PS1 section and change the PS1 variable as below. Note that this will remove the username and the hostname from the prompt, and shows the current directory in green and the git info in blue (just like in the PowerShell prompt). if [ "$color_prompt" = yes ]; then #PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ ' PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\w\[\033[34m\]$(__git_ps1 " (%s)")\[\033[00m\]\$ ' else PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ ' fi If you now open the prompt again the changes should be visible. === Bash from Git for Windows === If you use Git for Windows, you also get a bash prompt. To add git branch information to that prompt, you can use the same git-prompt.sh script as above, but the setup is a bit different. * cat /etc/bash.bashrc > ~/.bashrc * vi ~/.git-prompt.sh * Copy the content from https://raw.githubusercontent.com/git/git/master/contrib/completion/git-prompt.sh # Use the git-prompt script to allow for git information in the prompt # https://github.com/git/git/blob/master/contrib/completion/git-prompt.sh . ~/.git-prompt.sh Then, in the same file, locate the PS1 section and change the PS1 variable as below. Note that this will remove the username and the hostname from the prompt, and showes the current directory in green and the git info in blue (just like in the PowerShell prompt). #export PS1='\[\e]0;\w\a\]\n\[\e[32m\]\u@\h \[\e[35m\]$MSYSTEM\[\e[0m\] \[\e[33m\]\w\[\e[0m\]\n'"${_ps1_symbol}"' ' export PS1='\[\e]0;\w\a\]\n\[\e[32m\]\u@\h \[\e[35m\]$MSYSTEM\[\e[0m\] \[\e[33m\]\w\[\033[34m\]$(__git_ps1 " (%s)")\[\e[0m\]\n'"${_ps1_symbol}"' ' //This wiki has been made possible by://