Git aliases

JULY 7, 2022  ·  668 WORDS

If you use git in any capacity, you must have observed that a few commands are used more frequently than others. Similarly, for some commands that have many options associated with them, you would more often than not use the same options for your workflow. Typing the entire thing out every time isn't the best use of your time. Git aliases are a powerful workflow tool that will make your git experience much more enjoyable.

Setting up a git alias is quite easy. For example, to set ci as an alias for commit, you would need to run:

git config --global alias.ci commit

Going forward, you can use ci and commit interchangeably in your workflow. If you want to delete the ci alias, all you need to do is:

git config --global --unset alias.ci

config scope

The --global is the scope of the configuration. Some commonly used scopes are:

  • --local applies to a specific git repository and is used as default if no flag is provided.
  • --global applies to a user and is stored in the home directory ~/.gitconfig.
  • --system applies to all users on a machine.

For most use cases, --global works best because it allows you to use your aliases across different repositories but doesn't restrict other users to your custom workflow.

advanced tricks

While simply aliasing commands already saves a lot of time in the long run, it can be taken a step further with !. Prefacing commands with ! tells git to run it as a shell command and allows the creation of aliases that combine multiple commands. One thing to note here is that when these commands are run, they set the working directory to the top level and not the current directory.

For my workflow, when I checkout the main branch, I also try to pull the latest changes -- git checkout main followed by git pull. This can be combined into an alias as:

git config --global alias.main !git co main && git pull

Note that I am actually using co instead of checkout. Git allows you to set an alias in other aliases.

Since it is ultimately a shell command, we can also set an alias as a bash function with positional parameters. This enables writing complex actions as simple aliased commands. For example, the alias to clean merged branches (referenced from haacked) can be written as:

bclean = "!f() { \
DEFAULT=$(git default); \
git branch --merged ${1-$DEFAULT} \
| grep -v " ${1-$DEFAULT}$" \
| xargs git branch -d; \
}; \
f; \
unset f"

NOTE: I added the \ for readability. This can all be added in a single line however, a case can be made to add a git-newcommand instead when the alias becomes too complicated.

recommended aliases

While this ultimately depends on your workflow, here are some sensible aliases for commonly used actions. You can directly copy this snippet to your ~/.gitconfig:

[alias]
ci = commit -m
cv = ci --verbose
co = checkout
st = status
br = branch
# checkout 'main' branch and pull latest changes
main = !git co main && git pull
# see your staged changes
staged = diff --cached
# unstage a specific file. usage: git unstage <filepath>
unstage = reset --
# reset the last commit
undo = reset HEAD~ --mixed
# list all aliases
aliases = !git config --get-regexp alias | cut -c 7-

The main takeaway is to keep an eye out for commands you use regularly, add aliases and make a conscious effort to use them in your workflow. Aliases will start paying dividends before you even realize it.