A Machine Learning Engineer's Homepage

It is not just about machine learning ...

Home Blogs LinkedIn Publications

Displaying Git Branch in Your Shell Prompt

When you’re deep in a Git project, constantly checking which branch you’re on with git branch can slow you down. Wouldn’t it be awesome if your shell prompt could show the current branch name, highlighted in a bright, eye-catching color? This simple tweak saves time and keeps you focused on coding. In this friendly guide, we’ll show you how to set up your shell prompt to display the Git branch in cyan for Zsh, Bash, and PowerShell. We’ll provide the code, explain how to configure it, break down each line, and show example outputs so you know exactly what to expect.

Zsh Setup

Configuration File

To customize the Zsh prompt, modify the ~/.zshrc file, which is the configuration file for Zsh.

Code

Add the following code to your ~/.zshrc:

function parse_git_branch() {
    git branch 2> /dev/null | sed -n -e 's/^\* \(.*\)/[\1]/p'
}
COLOR_DEF=$'%f'
COLOR_GIT=$'%F{51}'
setopt PROMPT_SUBST
export PROMPT='%n@%m %1~ ${COLOR_GIT}$(parse_git_branch)${COLOR_DEF} %# '

Line-by-Line Explanation

Setup Instructions

  1. Open your ~/.zshrc file in a text editor (e.g., nano ~/.zshrc).
  2. Append the code above to the file.
  3. Save and close the file.
  4. Reload the Zsh configuration by running source ~/.zshrc or restarting your terminal.

Example Output

If you’re in the ~/your-project directory on the main branch, your prompt will look like this (with [main] in cyan):

user@machine ~/your-project [main] %

Bash Setup

Configuration File

To customize the Bash prompt, modify the ~/.bashrc file (or ~/.bash_profile on some systems).

Code

Add the following code to your ~/.bashrc:

parse_git_branch() {
  git branch 2>/dev/null | sed -n -e 's/^\* \(.*\)/[\1]/p'
}
COLOR_DEF="\[\033[0m\]"
COLOR_GIT="\[\033[38;5;51m\]"
export PS1="\u@\h \w ${COLOR_GIT}\$(parse_git_branch)${COLOR_DEF} \$ "

Line-by-Line Explanation

Setup Instructions

  1. Open your ~/.bashrc file in a text editor (e.g., nano ~/.bashrc).
  2. Append the code above to the file.
  3. Save and close the file.
  4. Reload the Bash configuration by running source ~/.bashrc or restarting your terminal.

Example Output

If you’re in the ~/your-project directory on the main branch, your prompt will look like this (with [main] in cyan):

user@machine ~/your-project [main] $

PowerShell Setup

Configuration File

To customize the PowerShell prompt, modify your PowerShell profile file. On Windows, this is typically located at ~\Documents\WindowsPowerShell\profile.ps1 or ~\Documents\PowerShell\profile.ps1 (for PowerShell 7). If the profile file doesn’t exist, you can create it.

Code

Add the following code to your PowerShell profile file:

function Get-GitBranch {
    try {
        $branch = git branch --show-current 2>$null
        if ($branch) {
            return "[$branch]"
        }
        return ""
    } catch {
        return ""
    }
}
$COLOR_DEF = "`e[0m"
$COLOR_GIT = "`e[38;5;51m"
function Prompt {
    $user = $env:USERNAME
    $host = $env:COMPUTERNAME
    $path = (Get-Location).Path
    $gitBranch = Get-GitBranch
    "${user}@${host} ${path} ${COLOR_GIT}${gitBranch}${COLOR_DEF} $ "
}

Line-by-Line Explanation

Setup Instructions

  1. Determine your PowerShell profile file location by running echo $PROFILE in PowerShell.
  2. If the file doesn’t exist, create it with New-Item -Path $PROFILE -Type File -Force.
  3. Open the profile file in a text editor (e.g., notepad $PROFILE).
  4. Append the code above to the file.
  5. Save and close the file.
  6. Reload the profile by running . $PROFILE or restarting PowerShell.

Example Output

If you’re in the C:\Users\user\your-project directory on the main branch, your prompt will look like this (with [main] in cyan):

user@machine C:\Users\user\your-project [main] $

Summary

This tutorial showed you how to add a splash of color and functionality to your shell prompt by displaying the current Git branch in cyan for Zsh, Bash, and PowerShell. We provided the code for each shell, explained how to set it up in the appropriate configuration files (~/.zshrc, ~/.bashrc, or the PowerShell profile), broke down each line, and included example outputs to show what your prompt will look like in action. Note that you may need to tweak the code slightly to fit your environment, such as adjusting the color codes (e.g., changing 51 to another number for a different color) or prompt format to match your preferences or terminal settings. With these steps, you can streamline your workflow and keep your Git branch front and center, all while adding a touch of style to your terminal!