Show your current git branch in your Bash terminal prompt

I recently found this little nugget and had to share. This setup allows you to see your current git branch in your terminal prompt.
To set this up:

  • Download https://github.com/git/git/blob/master/contrib/completion/git-completion.bash and save it where you please. I saved it as ~/.git-completion.sh.
  • Add the following to your ~/.bash_profile

source ~/.git-completion.sh
PS1='[\u@\h \W$(__git_ps1 " (%s)")]\$ '

  • Enter the following in your terminal session: $ source ~/.bash_profile

Your prompt will look like the following example when in a git repo: [user@host current-folder (git-branch)]. You can customize the prompt configuration as you wish.

Enjoy!

Blog Category: 

Add new comment