Another cool feature Git provides is to have the prompt display status information if the current working directory is a Git repository.
For the status information prompt to work, we also need to source another file, git-prompt.sh
, which is usually distributed with the Git installation and located in the same directory as the completion file.
In your .bashrc
or .zshrc
file, add the following code snippet, again depending on your shell and the location of the git-prompt.sh
file:
if [ -f /etc/bash_completion.d/git-prompt.sh ]; then source /etc/bash_completion.d/git-prompt.sh fi
To make use of the command prompt, we must change the PS1
variable; usually this is set to something like this:
PS1='u@h:w$ '
The preceding command shows the current user, an @
sign, the host name, the current working directory relative to the user's home directory, and finally a $
character:
aske@yggdrasil:~/cookbook-tips-tricks$
We can change this to add a branch name after the working directory by adding $(__git_ps1 " (%s)")
to the PS1
variable:
PS1='u@h:w$(__git_ps1 " (%s)") $ '
Our prompt will now look like this:
aske@yggdrasil:~/cookbook-tips-tricks (master) $
It is also possible to show the state of the working tree, index, and so on. We can enable these features by exporting some environment variables in the .bashrc
file that git-prompt.sh
picks up.
The following environment variables can be set:
Let's try to set some of the variables in the ~/.bashrc
file:
export GIT_PS1_SHOWUPSTREAM=auto export GIT_PS1_SHOWDIRTYSTATE=enabled PS1='u@h:w$(__git_ps1 " (%s)") $ '
Let us see the ~/.bashrc
file in action:
aske@yggdrasil:~ $ cd cookbook-tips-tricks/ aske@yggdrasil:~/cookbook-tips-tricks (master=) $ touch test aske@yggdrasil:~/cookbook-tips-tricks (master=) $ git add test aske@yggdrasil:~/cookbook-tips-tricks (master +=) $ echo "Testing" > test aske@yggdrasil:~/cookbook-tips-tricks (master *+=) $ git commit -m "test" [master 5c66d65] test 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 test aske@yggdrasil:~/cookbook-tips-tricks (master *>) $
When using the __git_ps1
option, Git will also display information when merging, rebasing, bisecting, and so on. This is very useful and a lot of git status
commands suddenly become unnecessary as you have the information right there in the prompt.
What is a terminal these days without some colors? The git-prompt.sh
script also supports this. All we need to do is set the GIT_PS1_SHOWCOLORHINTS
variable to a nonempty value and instead of using PS1
, we need to use PROMPT_COMMAND
. Let's change ~/.bashrc
:
export GIT_PS1_SHOWUPSTREAM=auto export GIT_PS1_SHOWDIRTYSTATE=enabled export GIT_PS1_SHOWCOLORHINTS=enabled PROMPT_COMMAND='__git_ps1 "u@h:w" "\$ "'
If we redo the same scenario as the previous one, we get the following:
aske@yggdrasil:~$ cd cookbook-tips-tricks/ aske@yggdrasil:~/cookbook-tips-tricks (master=)$ touch test aske@yggdrasil:~/cookbook-tips-tricks (master=)$ git add test aske@yggdrasil:~/cookbook-tips-tricks (master +=)$ echo "Testing" > test aske@yggdrasil:~/cookbook-tips-tricks (master *+=)$ git commit -m "test" [master 0cb59ca] test 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 test aske@yggdrasil:~/cookbook-tips-tricks (master *>)$
If you are using zsh
or just want to try something new with many features, such as completion, Git support, and so on, you should take a look at the oh-my-zsh
framework available for zsh
at https://github.com/robbyrussell/oh-my-zsh.