Bash

Show Current Branch in Prompt

admin by @admin ADMIN
5d ago
May 31, 2026
Public
0 0 up · 0 down Sign in to vote
Append the git branch to your bash prompt. Survives non-git dirs gracefully (just shows nothing). Drop into ~/.bashrc.
Bash
Raw
git_branch() {
    git symbolic-ref --short HEAD 2>/dev/null
}

# Append to PS1 — change colors as you like
export PS1='\u@\h \w \[\e[36m\]$(git_branch)\[\e[0m\]\$ '

# With "dirty" indicator (* if there are uncommitted changes)
git_dirty() {
    [[ -n "$(git status --porcelain 2>/dev/null)" ]] && echo "*"
}
export PS1='\u@\h \w \[\e[36m\]$(git_branch)$(git_dirty)\[\e[0m\]\$ '
Tags

Save your own code snippets

Create a free account and build your private vault. Share publicly whenever you want.