Monday, February 9, 2015

Git status in bash prompt - bash-git-prompt

Do you use Git?


Have you ever committed a change into a wrong branch? Have you ever started working on a feature/bug fix without pulling from remote and thus need to rebase and resolve conflicts later? Have you ever forgotten your stashed changes?

With bash-git-prompt tool that's gone!

The tools


Recently I was looking for some tool which would help me to avoid the problems mentioned above.

At first I tried sexy-bash-prompt, but I didn't like the default scheme, it was too different to the usual openSUSE bash command prompt. I tried to customize it, although the code allows some customization I could not make it look like the openSUSE default.

Then I tried bash-git-prompt. This tool is similar to sexy-bash-prompt, but provides more details about the git status. What I really like:

  • It prints more details about the current Git status (number of commits behind/ahead of remoter, count of stashed changes,...)
  • It periodically fetches the status from remote, if there are new commits on the remote you will be noticed (the check is run every 5 minutes, but it's configurable and can be completely disabled)
  • Displays the exit status of the last command, if a command fails you'll see a red X mard and the exit status number. So a failure is more visible on the terminal.
I really recommend to give it a try if you use Git from command line, it can save you same headache...

Installation


Installation is really easy, just run

cd ~
git clone https://github.com/magicmonty/bash-git-prompt.git .bash-git-prompt

If you want to update it later just run "git pull" in that directory.


openSUSE theme


Again the default style is too different, but it turned out that the color and the style can easily changed and bash-git-prompt already provides several styles out of box.

So I decided to create a new style which would mimic the default openSUSE bash prompt as close as possible so you would not notice that you are using bash-git-prompt if you are outside a Git repository.

To use bash-git-prompt with the openSUSE theme simply add this to your ~/.bashrc file:

GIT_PROMPT_THEME=Single_line_openSUSE
source ~/.bash-git-prompt/gitprompt.sh

The next time you start a new shell session bash-git-prompt will be automatically loaded.

Except the exit status at the beginning of the line it looks like the usual openSUSE bash prompt.

Let's see how it can help with Git:





Note: If you do not like the exit status indicator at the beginning of the line then use Single_line_NoExitState_openSUSE theme instead.

Help


You can run git_prompt_help to see what the used symbols mean:

 There are also some examples displayed by git_prompt_examples command:

And if you want to change the colors used in the style you can use git_prompt_color_samples to see all colors available (note: this depends on the terminal configuration, the colors might not be the same in a different terminal):

Note for Midnight Commander Users


I noticed that bash-git-prompt does not work correctly when you use Midnight Commander ("mc"). The problem is that it overwrites the PROMPT_COMMAND shell variable (see more details at

My workaround it to define an alias in ~/.alias file:

alias gp="source ~/.bash-git-prompt/gitprompt.sh"

and run "gp" whenever you want to use the bash-git-prompt functionality in mc subshell.

(The ticket above contains a patch for mc, but you would need to recompile it from sources and maintain it yourselves, i.e. keep it up to date if a security fix is released...)

10 comments:

Unknown said...

Thanks, looks like i'm updating my prompts again. I was using something similar but not as good based off http://mediadoneright.com/content/ultimate-git-ps1-bash-prompt but i customised it alot to support my history / session tool. http://simotek.net/tech/projects/opensuse-e/hiset-multiple-history-files-and-sessions-for-bash-and-other-tinkering/

Ackerley Nelson said...
This comment has been removed by a blog administrator.
Ladislav said...

JFYI: the Midnight Commander ticket has been closed as fixed after 5 years (yay!), see https://www.midnight-commander.org/ticket/2027. The workaround mentioned in the end of the article should not be needed with the latest Midnight Commander. (However I haven't tested it yet.)

Anonymous said...

For Hadoop Trainingn in Bangalore - Hadoop Training in bangalore

Unknown said...

Issues regarding epson printers can be easily resolved by Downloading the 0x97 Fix patch which resolves major issues associated with epson printers.

HEMA said...


Thanks for your blog!!
Selenium Automation Training in Bangalore
Selenium Training Institutes in Bangalore
Selenium Training in Marathahalli
Best Selenium Automation Training in Bangalore
Selenium Training in Marathahalli
Best Selenium Training in Bangalore
Selenium Software Training in Bangalore
Selenium Training Institutes in Bangalore
Top 10 selenium training institutes in bangalore

Buy Seo Service said...

Thanks for such a wonderful content. Our Motive is not just to create links but to get them indexed as will
Increase Domain Authority (DA).We’re on a mission to increase DA PA of your domain
High Quality Backlink Building Service
Boost DA upto 15+ at cheapest
Boost DA upto 25+ at cheapest . Very Helpful

Linkfeeder said...

Annabelle loves to write and has been doing so for many years.Cheapest and fastest Backlink Indexing Best GPL Store TECKUM IS ALL ABOUT TECH NEWS AND MOBILE REVIEWS

Tec said...

KGF 2 : Directed by Prashanth Neel. With Yash, Sanjay Dutt, Raveena Tandon, Prakash Raj. The blood-soaked land of Kolar Gold Fields

Tec said...

Full Form