Over a million developers have joined DZone.

Displaying Git branch in Linux command prompt

· Java Zone

Navigate the Maze of the End-User Experience and pick up this APM Essential guide, brought to you in partnership with CA Technologies

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.

But this beautiful Git presentation (download) by Luca Milanesio of GitEnterprise and HudsonMobi provided a better solution! Take a look:

 

[~/projects]$ git clone git://git.kernel.org/pub/scm/git/git.git
[~/projects]$ cd git
[~/projects/git]$ git checkout v1.7.5.4
[~/projects/git]$ make prefix=/usr/local all
[~/projects/git]$ sudo make prefix=/usr/local install
[~/projects/git]$ git --version
git version 1.7.5.4
[~/projects/git]$ source contrib/completion/git-completion.bash
[~/projects/git]$ export PS1='\W$(__git_ps1 " (%s)") \$ '
git ((v1.7.5.4)) $

Now my command prompt always displays current Git branch. Let’s try TeamCity build agent checkout folder:

git ((v1.7.5.4)) $ cd ~/java/agent/work/56ff0db610964031/
56ff0db610964031 (master) $

It works! Many thanks, Luca.

#[12:48:47][~/projects/git] ((v1.7.5.4)) $ cat ~/.bash_profile | grep PS1
PS1='#[\t][\w]$(__git_ps1 " (%s)") \$ '
#[12:49:45][~/projects/git] ((v1.7.5.4)) $ cd
#[12:52:40][~] $

From http://evgeny-goldin.com/blog/displaying-git-branch-linux-command-prompt

Thrive in the application economy with an APM model that is strategic. Be E.P.I.C. with CA APM.  Brought to you in partnership with CA Technologies.

Topics:

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}