Skip to content

Instantly share code, notes, and snippets.

@kenichi-shibata
Created May 13, 2016 06:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kenichi-shibata/879f8d78bc905094fd712280c1445813 to your computer and use it in GitHub Desktop.
Save kenichi-shibata/879f8d78bc905094fd712280c1445813 to your computer and use it in GitHub Desktop.
Add branch name to terminal git (MacOS X)

Add branch name in terminal

add this in ~/.bash_profile

Git branch in prompt.

parse_git_branch() {

    git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'

}

export PS1="\u@\h \W\[\033[32m\]\$(parse_git_branch)\[\033[00m\] $ "
@kenichi-shibata
Copy link
Author

Notes:

Depending on configuration changes you may have made previously you may already have a PS1 variable being exported. In this case you will need to add \$(parse_git_branch) somewhere in the existing value.

The \[\033[32m\] parts set the color of the branch text. If you prefer another color check online for a reference on valid values.

The \ in \$(parse_git_branch) is important to ensure the function is called each time the prompt is displayed; without it, the displayed branch name would not be updated when, for example, checking out a different branch or moving in and out of a Git repository directory.`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment