DEV Community

Cover image for Git Aliases: a time-saving secret weapon for improved workflow and productivity
Daniel Genezini
Daniel Genezini

Posted on • Originally published at blog.genezini.com on

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

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>]"
Enter fullscreen mode Exit fullscreen mode

For example, to create an alias c for the checkout command in the global scope we use:

git config --global alias.c "checkout"
Enter fullscreen mode Exit fullscreen mode

If the command has quotes, we can use single quotes instead. For instance:

git config --global alias.ec "commit --allow-empty -m 'Empty commit'"
Enter fullscreen mode Exit fullscreen mode

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.

[user]
    name = ...
    email = ...

...

[alias]
    c = checkout
    ec = commit --allow-empty -m 'Empty commit'
Enter fullscreen mode Exit fullscreen mode

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"
Enter fullscreen mode Exit fullscreen mode

⚠ī¸ 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`

ℹī¸ Some of these commands were explained in this post and this post. Please read the posts for more details.

Liked this post?

I post extra content in my personal blog. Click here to see.

Follow me

References and Links

Top comments (0)