Git-completion and git-prompt are scripts that provide versatile completion support as well as visualization of current branch and status when working from the command line. Even though I work with git daily I hadn’t bumped into these scripts until quite recently. They have proven to be very useful, so I decided to share this tip.

The installation is very straightforward. The scripts just need to be sourced each time the shell is started. Most shells have a configuration file that is automatically loaded upon each start-up. With Bash it is usually ~/.bashrc file. However, depending on the Linux distribution and shell, the configuration file may differ. It is usually in some dotfile in the user’s home directory.

The scripts are part of the official git project, and they are found from this GitHub link. Personally I keep all the custom Bash scripts in a folder ~/.bash_custom/. First, copy git-completion.bash and git-prompt.sh from the link above to your home directory (e.g. to .bash_custom directory).

The following snippet from the ~/.bashrc file shows the relevant part to enable the scripts. The first three lines enable additional features from the scripts, which are pretty self-explanatory. The next part sets a command that is called each time the prompt is printed. This will make sure the information in the prompt is updated when git and other commands are executed. This part is Bash specific. If other shell, such as ZSH, is used see the comments in git-prompt.sh script. Finally, the for-loop loads (sources) all scripts in the .bash_custom directory including the git scripts previously copied there.

export GIT_PS1_SHOWUNTRACKEDFILES=true export GIT_PS1_SHOWCOLORHINTS=true export GIT_PS1_SHOWDIRTYSTATE=true # will show username, at-sign, host, colon, # current directory, and git status followed by dollar export PROMPT_COMMAND='__git_ps1 "\u@\h:\w" "\\\$ "' for f in ~/.bash_custom/*; do . $f done

Note. if your terminal has colored prompt, the PROMPT_COMMAND shown above will remove the coloring. To retain the coloring look how the prompt is configured before adding the new PROMPT_COMMAND (in Bash look for variable PS1). Below is an example from Ubuntu that will preserve the colors.

export PROMPT_COMMAND='__git_ps1 "${debian_chroot:+($debian_chroot)}\[\033[01;32m\] \u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]" "\$ "'

When used in PROMPT_COMMAND, the __git_ps1 takes two parameters. What is printed before and after the git status part respectively.

export PROMPT_COMMAND='__git_ps1 "AAA" "BBB:"'

For instance the command above would produce the following prompt (not very fancy, but it shows how the parameters work):

That is all that is needed for installation. Now the scripts are ready to be tested. The image below shows examples of the git-prompt in action:

When the current directory does not contain a git repository the prompt looks completely normal. When the repository is entered, the prompt starts to display the current branch and status. The example above shows:

Switching branch Having untracked files which are denoted with %-symbol (GIT_PS1_SHOWUNTRACKEDFILES) Modified files that were already tracked by git, denoted with *-symbol (GIT_PS1_SHOWDIRTYSTATE) Staged changes, denoted with +-symbol Rebase status when stopped for amending

There are also other options available. For instance, GIT_PS1_SHOWUPSTREAM option shows whether the local repository is ahead, behind or diverged from the upstream repository. GIT_PS1_SHOWUPSTREAM = “auto verbose name”. Would show the following information. Personally I think that at least the verbose option starts to make the prompt too crowded.

The next figure shows a couple of examples of the completion.Basically it just works as you would expect. Type a command and hit tabulator to display the completion suggestions.

The completion is quite versatile. It shows for instance, local and remote branches, sub-commands and long options.

All in all, settings these two scripts up literally takes a couple of minutes, but it can make working with git command line much more enjoyable.