How to show the current git branch when you are using zshrc?

Update the .zshrc file with the following code

# Enable prompt substitution
setopt PROMPT_SUBST

# Function to fetch git branch name
git_info() {
    ref=$(git symbolic-ref HEAD 2> /dev/null) || return
    echo " %F{yellow}("${ref#refs/heads/}")%f"
}

# Main Prompt
PROMPT='%n@%m %F{green}%~$(git_info)%f $ '

Then, run $ source ~/.zshrc

So that you can check that the current branch will be displayed on the command line. Example

Leave a comment