Introduction
Git is a powerful tool with lots of commands and customizable options. Fortunately, to make our lives easier, we can create aliases to simplify these commands.
In this post I’ll explain how Git aliases work and show the aliases I currently use.
Git configuration scopes
Git has 3 levels of configuration scope. Alias are created in the git configuration, so they will work in the scope in which they were created.
Name | Scope | Config file |
---|---|---|
local | Applies only on the current repository. | Unix:[repo]/.git/config Windows: [repo]\.git\config |
global | Applies for the current user. | Unix:~/gitconfig Windows: %USERPROFILE%\.gitconfig |
system | Applies for all users of the system. | Unix:/etc/gitconfig Windows: %PROGRAMDATA%\Git\config |
Git aliases
Git aliases are custom commands we can create in Git.
We can create an alias using the git config
command or editing the git config file.
Creating an alias with the git config command
To create an alias with the git config
command we use the following syntax:
git config --[<scope>] alias.[<alias_name>] "[<git command>]"
For example, to create an alias c
for the checkout
command in the global
scope we use:
git config --global alias.c "checkout"
If the command has quotes, we can use single quotes instead. For instance:
git config --global alias.ec "commit --allow-empty -m 'Empty commit'"
Creating an alias in the git config file
We can create aliases directly in the git config file. Look here to see the file location for each configuration scope.
Just add the alias and the command in the alias
section of the file. If the section doesn’t exist, just create it.
|
|
Complexes aliases
Git allows us to use shell commands in his aliases. When we start an alias with !
, we can run any shell command, allowing us to use multiple commands with &&
or |
.
In this example, I define a function f()
that gets all commit hashes and pass then to the git grep
command in addition to receiving a string parameter to search for in the files:
git config --global alias.search "!f() { git rev-list --all | xargs git grep $1; }; f"
⚠️ Note that we need to use the
git
command in these aliases because we are passing general shell commands.
My aliases
These are the aliases that I currently use on a daily basis.
Alias | Command | How to use |
---|---|---|
c | checkout | git c develop |
f | fetch --prune | git f |
ec | commit --allow-empty -m 'Empty commit' | git ec |
graph | log --graph --oneline | git graph |
wp | worktree prune | git wp |
wa | !f() { git worktree prune && git worktree add $1 $2; }; f | git wa ../folder branch-name |
search | !f() { git rev-list --all | xargs git grep $1; }; f | git search keyword |
mb | !f() { git branch --show-current | xargs git merge-base $1; }; f | git mb origin/main |
reset-to-base | !f() { git mb $1 | xargs git reset; }; f | git reset-to-base origin/main |
ℹ️ Some of these commands are explained in this post and this post. Please read the posts for more details.
ℹ️
mb
andreset-to-base
are explained in this post.