Gingle Bells!  It’s time to pimp your git prompt. To show off but also to have plenty of information on hand that will make our life easier on the command line and on a daily basis.

 

Use a git contribution : git-prompt

We will use a contribution from the official git repository: git-prompt. It works for bash and zsh, on Linux but also on Git for Windows. If you move to a git repository directory, it allows you to display notifications in the prompt for the following cases:

    • untracked files detection in the working area (%)
    • modified files detection in the working area (*)
    • indexed modifications detected (+)
    • one or more stash zones detected ($)
    • display of the status of the local branch compared to the remote branch (<, =,>)

This script is provided by default by Git for Windows and most Linux distributions (in / usr / share / doc / git-core or /etc/bash_completion.d). It includes functions and variables to display all those  information.

Finally we have to integrate it into the prompt definition, ie the variable PS1.

 

Modifying your prompt

We are going to modify this PS1 variable to integrate more information:

  1. Source the git-prompt.sh script to load variables and functions into memory
  2. Enable variables to add notifications to the prompt
  3. Redefine the PS1 environment variable (prompt)

 

[ennael@zarafa ~]$ cat ~/.bashrc

# .bashrc

# Sourcer le fichier git-prompt (adapter le nom et le chemin du script)
. /etc/bash_completion.d/git-prompt.sh

# Activer les variables complémentaires

# afficher le status de la branche locale (<, >, =)
export GIT_PS1_SHOWUPSTREAM=1
# affiche (*) en cas de modifications en zone de travail ou de cache
export GIT_PS1_SHOWDIRTYSTATE=1
# affiche (%) en cas de nouveaux fichiers
export GIT_PS1_SHOWUNTRACKEDFILES=1
# affiche ($) lorsqu’au moins une zone de stash existe
export GIT_PS1_SHOWSTASHSTATE=1

# Construction du prompt utilisant la fonction git_ps1 avec colorisation
export PS1=’\e[32m[\u@\h \W\e[34m$(__git_ps1 ” \e[33m(%s)”)\e[32m]$ \e[39m

Load .bashrc file content and here we go!

[ennael@zarafa ~]$ . ~/.bashrc

git-prompt as an example

Here are some examples about this new prompt.

 

Untracked files detection

[ennael@zarafa main (devel %)]$ 

Pending modifications in working area or staging area

[ennael@zarafa main (devel *+)]$ 

Stash area(s) detection

[ennael@zarafa main (devel $)]$ 

Local branch status: 3 new commits available in  remote branch, 1 specific commit in local branch

[ennael@zarafa main (devel u+3-1)]$ 

Amazing! Let’s sum up.

Look for git-prompt script or download it

git worktree add <répertoire> <branche>

Modify ~/.bashrc file and reload it

. ~/.bashrc

Use it !