Featured image of post Git Aliases: a time-saving secret weapon for improved workflow and productivity

Git Aliases: a time-saving secret weapon for improved workflow and productivity

Save time and your fingers with this tip

Follow me

Introduction Link to this section

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 Link to this section

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.

NameScopeConfig file
localApplies only on the current repository.Unix:
[repo]/.git/config
Windows:
[repo]\.git\config
globalApplies for the current user.Unix:
~/gitconfig
Windows:
%USERPROFILE%\.gitconfig
systemApplies for all users of the system.Unix:
/etc/gitconfig
Windows:
%PROGRAMDATA%\Git\config

Git aliases Link to this section

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 Link to this section

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 Link to this section

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.

1
2
3
4
5
6
7
8
9
[user]
    name = ...
    email = ...

...

[alias]
    c = checkout
    ec = commit --allow-empty -m 'Empty commit'

Complexes aliases Link to this section

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 Link to this section

These are the aliases that I currently use on a daily basis.

AliasCommandHow to use
ccheckoutgit c develop
ffetch --prunegit f
eccommit --allow-empty -m 'Empty commit'git ec
graphlog --graph --onelinegit graph
wpworktree prunegit wp
wa!f() { git worktree prune && git worktree add $1 $2; }; fgit wa ../folder branch-name
search!f() { git rev-list --all | xargs git grep $1; }; fgit search keyword
mb!f() { git branch --show-current | xargs git merge-base $1; }; fgit mb origin/main
reset-to-base!f() { git mb $1 | xargs git reset; }; fgit 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 and reset-to-base are explained in this post.

💬 Like or have something to add? Leave a comment below.
Ko-fi
GitHub Sponsor
Licensed under CC BY-NC-SA 4.0
Built with Hugo
Theme Stack designed by Jimmy