Skip to content

Instantly share code, notes, and snippets.

@adamheins
Created September 21, 2015 06:16
Show Gist options
  • Save adamheins/ea63e197bef97091c535 to your computer and use it in GitHub Desktop.
Save adamheins/ea63e197bef97091c535 to your computer and use it in GitHub Desktop.
Get the current branch for your git repo in bash.
#/bin/bash
# If the cwd is a git repo, display the branch name in the tmux status bar.
get_git_branch() {
if [ "$(git rev-parse --is-inside-work-tree 2>/dev/null)" ]; then
local branch short_branch
branch=$(git symbolic-ref -q HEAD)
branch=${branch##refs/heads/}
branch=${branch:-HEAD}
short_branch=$(echo "$branch" | cut -c1-20)
if [ ${#branch} -gt ${#short_branch} ]; then
short_branch=${short_branch}...
fi
GIT_BRANCH=$short_branch
else
GIT_BRANCH=
fi
}
PROMPT_COMMAND="get_git_branch; $PROMPT_COMMAND"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment