Bash prompt with status information

Another cool feature Git provides is to have the prompt display status information if the current working directory is a Git repository.

Getting ready

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.

How to do it...

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

How it works…

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:

Variable

Value

Effect

GIT_PS1_SHOWDIRTYSTATE

Nonempty

Shows * for unstaged changes and + for staged changes

GIT_PS1_SHOWSTASHSTATE

Nonempty

Shows a $ character if something is stashed

GIT_PS1_SHOWUNTRACKEDFILES

Nonempty

Shows a % character if there are untracked files in the repository

GIT_PS1_SHOWUPSTREAM

auto

verbose

name

Legacy

Git

svn

Auto shows whether you are behind (<) or ahead (>) of the upstream branch. A <> value is displayed if the branch is diverged and = if it is up to date. Verbose shows the number of commits behind/ahead. Name shows the upstream name. Legacy is verbose for old versions of Git. Git compares HEAD to @{upstream}. SVN compares HEAD to svn upstream.

GIT_PS1_DESCRIBE_STYLE

contains

branch

describe

default

Displays extra information when on a detached HEAD. Contains is relative to a newer annotated tag (v1.6.3.2~35). Branch is relative to a newer tag or branch (master~4). Describe is relative to an older annotated tag (v1.6.3.1-13-gdd42c2f). Default is the tag that matches exactly.

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.

There's more…

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 *>)$

See also

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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset