git bash enhancements

created onJune 22, 2022
last modified onJune 28, 2022

using git aliases for git commands

Typing every commit sucks. Same for some other commands of git that you work with every day. That’s where aliases come in handy.

git aliases

git aliases are set in the git configuration. To set an alias for , run:

git config --global alias.cm commit -m

Now instead of running , you can run

git cm <commit message>

Of course, you can directly edit the git config file git config file to set an alias. For the example above:

[alias] cm = commit -m

bash aliases

With git aliases, you still have to type for every commit, which is way too much for me. This is why I prefer bash aliases over git aliases. I have a some aliases for git commands in my bashrc (the path for the bashrc in your home dir is usually ):

... alias ga='git add' alias gb='git checkout -b' alias gco='git checkout' alias gcm='git commit -m' alias gps='git push' alias gpu='git pull' alias gst='git status' ...

That boils down the commit command to:

gcm <commit message>

which is half of the length of an equivalent git alias, or, in total, three characters.

before defining an alias, check for programs with the same name as the alias you intend to define to avoid name clashes. Unix/Linux is notorious for two- or three-character program names.

git bash prompt

You can customize your bash prompt to show information about the repo’s current state like the current branch, the status of the working directory and if you are ahead or behind upstream. For this, you need the file from the contrib section of the git repo. To get this file:

  • check your git version with
  • clone the git repo from https://github.com/git/git and checkout the tag that matches our version of git. Alternatively (if you have no github account), download your git version’s tag.
  • from the cloned or downloaded repo, copy the file to some suitable dir on your machine. I put it into to make it easier to configure the git prompt for multiple users.

provides the function which can be called in your PS1 definition as command substitution. In your , source the file before the definition of (which controls your bash prompt):

source /usr/share/git-core/contrib/git-prompt.sh

change your PS1 to call __git_ps1 as command-substitution, i.e. with . A good place is to put it at the end of the prompt, before the ‘$’ character, that most prompts display before the cursor:

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

With the git prompt enhancements your bash prompt will look similar to this:

rudolf@idoru:/opt/intra/site/codebase(master *|u+1)$

In the example above, I’m on branch master, with some unstaged dchanges changes and one commit ahead of upstream.

The information that the git prompt displays can be configured with environment variables. Of these environment variables, the ones that I find most useful are listed below. For a complete list, check out the comments in .

environment variables to control git prompt

environment variable value effect
GIT_PS1_SHOWDIRTYSTATE any non-empty value displays ‘*’ for unstaged changes and ‘+’ for staged changes
GIT_PS1_SHOWSTASHSTATE any non-empty value displays ‘$’ if something is stashed
GIT_PS1_SHOWUNTRACKEDFILES any non-empty value displays ‘%’ if there are untracked files
GIT_PS1_SHOWUPSTREAM auto displays difference between HEAD and it’s upstream: ‘<’ if you are behind, ‘>’ if you are ahead, ‘<>’ if you have diverged and ‘=’ for no difference.
GIT_PS1_SHOWUPSTREAM verbose like above, but instead ‘<’, ‘>’, ‘<>’ and ‘=’, displays the number of commits you are ahead or behind, in the form of i.e. ‘|u+1’ if you are ahead one commit
GIT_PS1_SHOWCOLORHINTS colored hint about the current dirty state; the colors are based on the colored output of git status -sb. works always in zsh, but only works in bash when using __git_ps1 with PROMPT_COMMAND or precmd

git config variables to control git prompt

Additionally, the display of some of the information, configured with the env vars, can be switched on and off per repository with git config variables. The corresponding git config variables in the table below default to true, if the corresponding PS1 environment variable is set. To switch of the display of certain information, set the corresponding git config variable to false:

environment variable corresponding config variable
GIT_PS1_SHOWDIRTYSTATE bash.showDirtyState
GIT_PS1_SHOWUNTRACKEDFILES bash.showUntrackedFiles
GIT_PS1_SHOWUPSTREAM bash.showUpstream

git bash completion

the git repository on github contains an enhancement for bash completion in the contrib section.

  • check your git version with
  • clone the git repo from https://github.com/git/git and checkout the tag that matches our version of git. Alternatively (if you have no github account), download your git version’s tag.
  • from the cloned or downloaded repo, copy the file to some suitable dir on your machine. I put it into to make it easier to configure the git prompt for multiple users.

Source the file in your :

. /usr/share/git-core/contrib/git-completion.bash

Once that’s done, the git command provides bash completion like you are accustomed from bash in general. I.e., if you type and hit , bash will auto-complete to all git commands that start with che:

rudolf@idoru:/opt/intra/site/codebase(master)$ git che checkout cherry cherry-pick

the git bash completion feature only works if you really clone or download the tag from the git repo that matches your git version. I cheated when getting the file git-prompt.sh, downloading just the latest tag, which is way ahead of the git version I have installed. Worked well with the git prompt but for the git bash completion I really had to dig out the tag that corresponds to the git version I have installed on my machine.