There are times when I keep running git status
or git branch
to check which branch I’m on and I just found that I could do something to show the name of my current Git branch on my Terminal prompt, thanks to a colleague.
In the following screenshot I’m working in the directory “sample_app”, on Git branch that is named “static-pages”:
In Ubuntu, here’s how I did it.
Create .bash directory in Home:
mkdir ~/.bash
Go to the new directory
cd ~/.bash
Clone the git-aware project from GitHub:
git clone git://github.com/jimeh/git-aware-prompt.git
Edit your .bashrc file. I use Vim editor for this:
vim ~/.bashrc
Add the following to the top of your .bashrc:
1 2 |
export GITAWAREPROMPT=~/.bash/git-aware-prompt source "${GITAWAREPROMPT}/main.sh" |
If you want colored prompts, add the following to the bottom of your .bashrc:
1 |
export PS1="\${debian_chroot:+(\$debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\] \[$txtcyn\]\$git_branch\[$txtred\]\$git_dirty\[$txtrst\]\$ " |
Otherwise, if you doon’t like colors, use this instead:
1 |
export PS1="\${debian_chroot:+(\$debian_chroot)}\u@\h:\w \[$txtcyn\]\$git_branch\[$txtred\]\$git_dirty\[$txtrst\]\$ " |
Open a new Terminal tab or restart Terminal. Navigate to any directory that has a Git repository and your Ubuntu’s Terminal prompt should now show the current Git branch’s name.