Wednesday, April 27, 2016

How to add git branch name to the command line

How many times you run the git branch command to find out which branch you in?

Following will help you to overcome above hassle and have a quick glance at your branch name.



1. Open /home/<USER>/.bashrc file.
2. Add following to the bashrc, save and exit.
parse_git_branch() {
 git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1)/'
}
if [ "$color_prompt" = yes ]; then
 PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[01;31m\]$(parse_git_branch)\[\033[00m\]\$ '
else
 PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w$(parse_git_branch)\$ '
fi
unset color_prompt force_color_prompt
3. Open a new terminal window and browse your git repo :)

1 comment:

  1. Hi,
    Thank you for the information , I also recently google looking for a similar code, to show up which branch i'm currently working on when i'm in a git repository.
    and I found these 2 repos liquidprompt[1] and git-aware-prompt[2]

    git-aware-prompt dose a similar thing what you have mentioned here
    liquidprompt is a greate tool to make your bash in to smart terminal

    [1] : https://github.com/nojhan/liquidprompt
    [2] : https://github.com/jimeh/git-aware-prompt

    again Thanks for sharing this code :)

    ReplyDelete