/ GIT, PRODUCTIVITY

Creating Git shortcuts

There are two main use-cases for creating Git shortcuts:

  1. The command is used very often, and the shortcut will save a keystrokes every type:
    git commit --amend --no-edit
  2. The command is used very seldom, and it’s hard to remember the exact correct options:
    git log --graph \
            --abbrev-commit \
            --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset'

In both cases, there are different ways to create shortcuts over Git commands.

Shell alias

The easiest and most straightforward way is to use standard shell aliases:

alias gitamend='git commit --amend --no-edit'
This can get pretty ugly if options themselves contains simple quotes, as in the above git log command. To handle that specific case, the escape sequence for ' is '"'"'.

The benefit of the shell alias way is that it can do everything a shell can, e.g. piping commands, executing sub-shells, etc.

For example, the following is a shortcut I use: it checks out the next commit in the commit history - quite useful for demos to jump across commits:

alias gitnext='git checkout $(git rev-list --topo-order HEAD..master | tail -1)'

Git alias

The previous method is by no way Git specific. Yet, Git also offers an aliasing feature by itself, through the git config command. The syntax is quite straightforward:

git config alias.<shortcut> <command>

For example, I use this approach for the following shortcut:

git config alias.amend 'commit --amend --no-edit'

It’s now possible to simply call:

git amend

This way only configures the shortcut in the current Git repository. To be able to call it regardless of the repository, use the usual --global option:

git config --global alias.amend 'commit --amend --no-edit'

It’s also possible to call external binaries, by prefixing it with !:

git config alias.sourcetree '!/Applications/SourceTree.app/Contents/MacOS/Sourcetree'

Git extension/plugin/subcommand

The last method is the most powerful, but also the most brittle.

I didn’t even found where it’s referenced in the Git documentation, or even if it’s referenced at all. Pointers welcome.

Just create any executable or script on the $PATH, and name it git-<command> e.g.:

git-foo
#!/bin/bash
echo Hello  $1;

At that point, the previous script can be called like this:

git foo John

Conclusion

There are several ways to create shortcuts in Git: plain bash aliases, proper Git aliases, and aptly-named executables.

  1. The first way is not Git specific
  2. The second one is able to handle Git command and options combinations
  3. Last but not least, the third way allows to integrate any executable into the Git flow

To go further:

Nicolas Fränkel

Nicolas Fränkel

Developer Advocate with 15+ years experience consulting for many different customers, in a wide range of contexts (such as telecoms, banking, insurances, large retail and public sector). Usually working on Java/Java EE and Spring technologies, but with focused interests like Rich Internet Applications, Testing, CI/CD and DevOps. Also double as a trainer and triples as a book author.

Read More
Creating Git shortcuts
Share this