Displaying Git branch in Linux command prompt
Join the DZone community and get the full member experience.Join For Free
Even though I create "dev" branch for all my Git projects and perform all development work there, I need to make sure I’m still on the "dev" branch from time to time. "gs" is my alias to "git status" and I run it very frequently for that and other purposes.
[~/projects]$ git clone git://git.kernel.org/pub/scm/git/git.git
[~/projects]$ cd git
[~/projects/git]$ git checkout v184.108.40.206
[~/projects/git]$ make prefix=/usr/local all
[~/projects/git]$ sudo make prefix=/usr/local install
[~/projects/git]$ git --version
git version 220.127.116.11
[~/projects/git]$ source contrib/completion/git-completion.bash
[~/projects/git]$ export PS1='\W$(__git_ps1 " (%s)") \$ '
git ((v18.104.22.168)) $
Now my command prompt always displays current Git branch. Let’s try TeamCity build agent checkout folder:
git ((v22.214.171.124)) $ cd ~/java/agent/work/56ff0db610964031/
56ff0db610964031 (master) $
It works! Many thanks, Luca.
#[12:48:47][~/projects/git] ((v126.96.36.199)) $ cat ~/.bash_profile | grep PS1
PS1='#[\t][\w]$(__git_ps1 " (%s)") \$ '
#[12:49:45][~/projects/git] ((v188.8.131.52)) $ cd
Opinions expressed by DZone contributors are their own.