While looking at the GitHub help article on subtree merges, I noticed that the prompt shown in the examples also listed the current branch of the repo. Like this:

myhost:webl[master]$

Interested to implement this myself I found the git_completion.sh script, which provides all kinds of additional features in bash. Below is a short introduction that should help you to add the current branch to your bash prompt.

How to modify your bash prompt

The PS1 variable determines how the prompt looks. You can see your current PS1 variable using echo in a terminal.

echo $PS1

My output (mac) was \h:\W \u\$. (note the space at the end) \h is the first part of my hostname. \W the current working directory and \u my username. There are a lot of extra options. Some of which I've listed below. More information can be found at this geekstuff tutorial.

  • \d - Date
  • \h - Hostname up to the first period
  • \H - Hostname
  • \u - Username
  • \w - The current working directory
  • \W - The base name of the current working directory

Integrating git into your bash prompt

Most of the git integration can be handled by the .git_completion.sh script. Which you can download from GitHub. (Do so now). Place the file in your home directory and name it .git_completion.sh. Then add the following to your .bash_profile (mac) or .bashrc (everything else).

source .git_completion.sh

Then close en re-open your terminal. Type git and hit tab twice, code completion for git command should now be working. The git_completion script has many more completion options (e.g. branches and remotes), take a look at the notes in the script to learn more.

Putting it together

Showing the current branch can now be accomplished using the __git_ps1 command provided by the gitcompletion script. To do this, add the following line to your .bashrc (or `.bashprofile` if on a mac).

export PS1='[\u@\h \W$(__git_ps1 " (%s)")]\$ '

$() is used to signify a function (in this case __git_ps1. __git_ps1 takes a format string that describes the lay-out of the result. %s will be translated to the current branch name.
This line says that the PS1 variable is set to [<user>@<hostname> <working dir> <git_ps1 branch if available>]$. If you want, you can also show untracked and staged changes. To do so, modify your bash profile to:

export GIT_PS1_SHOWDIRTYSTATE=true
export GIT_PS1_SHOWUNTRACKEDFILES=true
export PS1='[\u@\h \W$(__git_ps1 " (%s)")]\$ '

This will result in directories being displayed

[user@host directory-name (master*)]$ 
[user@host directory-name (master+)]$ 
[user@host directory-name (master%)]$
  1. On the first line a tracked file was modified
  2. The second line indicates a tracked file was modified and staged
  3. The third line indicates that untracked files are in your tree

Combinations of these are possible.

Colorize!

I like to add some color to my prompt to increase readability. Explaining colors in bash is something for a different tutorial, but this is my .bash_profile, it colorizes the branch name (if a branch is available).

export GIT_PS1_SHOWDIRTYSTATE=true
export GIT_PS1_SHOWUNTRACKEDFILES=true
PS1='\h:\W$(__git_ps1 " [\[\e[0;32m\]%s\[\e[0m\]]")\$ '

And that's it!. More information on colors can be found at archlinux

Back to index

Comments:

65b7538436dd4bc59e240524382c7dfd
Thilinag, about 1 year ago

awesome!

You can sign in using Github if you want to comment