Git Aliases: a time-saving secret weapon for improved workflow and productivity
Tags: git, github, devops, productivity
Published: 2023-01-07 11:11:14

## 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: <br /> `[repo]/.git/config` <br /> Windows: <br /> `[repo]\.git\config` |
| global | Applies for the current user. | Unix: <br /> `~/gitconfig` <br /> Windows: <br /> `%USERPROFILE%\.gitconfig` |
| system | Applies for all users of the system. | Unix: <br /> `/etc/gitconfig` <br /> Windows: <br /> `%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:

```bash
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:

```bash
git config --global alias.c "checkout"
```

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

```bash
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](#git-configuration-scopes) 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.

```ini
[user]
    name = ...
    email = ...

...

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

## 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:

```bash
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` |

> ℹ️ Some of these commands were explained in [this post](https://blog.genezini.com/p/7-tips-for-improving-your-productivity-with-git/) and [this post](https://blog.genezini.com/p/working-on-multiple-git-branches-in-parallel/). Please read the posts for more details.

## Liked this post?

I post extra content in my personal blog. [Click here](https://blog.genezini.com/) to see.

## Follow me

- [On Feedly](https://feedly.com/i/subscription/feed/https://blog.genezini.com/index.xml)
- [RSS](https://blog.genezini.com/index.xml)
- [Linkedin](https://www.linkedin.com/in/danielgenezini/)
- [Twitter](https://twitter.com/GeneziniDaniel)

## References and Links

- https://git-scm.com/book/en/v2/Git-Basics-Git-Aliases
- https://www.atlassian.com/blog/git/advanced-git-aliases