created on | June 22, 2022 |
last modified on | June 28, 2022 |
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 are set in the git configuration. To set an alias for , run:
Now instead of running , you can run
Of course, you can directly edit the git config file git config file to set an alias. For the example above:
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 ):
That boils down the commit command to:
which is half of the length of an equivalent git alias, or, in total, three characters.
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:
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):
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:
With the git prompt enhancements your bash prompt will look similar to this:
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 variable | value | effect |
---|---|---|
GIT_PS1_SHOWDIRTYSTATE | 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 |
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 |
the git repository on github contains an enhancement for bash completion in the contrib section.
Source the file in your :
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