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:
Going forward, you can use
commit interchangeably in your workflow. If you want to delete the
ci alias, all you need to do is:
--global is the scope of the configuration. Some commonly used scopes are:
--localapplies to a specific git repository and is used as default if no flag is provided.
--globalapplies to a user and is stored in the home directory
--systemapplies 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.
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:
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:
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.
While this ultimately depends on your workflow, here are some sensible aliases for commonly used actions. You can directly copy this snippet to your
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.