Tom Krush

Tom Krush

Dad, Husband, Programmer, Amateur Woodworker

18 May 2020

Get Faster With Git Aliases

A Git alias allows you to create shortcuts to simplify more complicated or less memorable commands.

Undo Your Commit

We all make mistakes. Most programs offer an undo feature. Let’s add an undo function to Git.

In the command line enter:

$ git config --global alias.undo "reset HEAD~1 --mixed"

Now, if you make a mistake and you want to undo it you can type git undo. This command will remove the last commit and keep your changes in the working copy.

Anatomy of Aliases

$ git config --global alias.{aliasName} "{command}"

This command will add an alias to your global ~/.gitconfigfile. Replace{aliasName}with your command name. Replace{command}` with your command.

To edit an existing alias, edit your ~/.gitconfig file.

Listing all Aliases

Before we get far, let’s give you an alias that lists all aliases.

$ git config --global alias.alias "! git config --get-regexp ^alias\\. | sed -e s/^alias\\.// -e s/\\ /\\ =\\ /"

If you forget what aliases you have, you can now type git alias.

Logging Commit

Logging commits in Git is an essential skill. Unfortunately, to get something that resembles a readable graph, you need to enter a cryptic command.

$ git log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit

In the command line enter:

$ git config --global alias.smartlog 'log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit'

You can now type git smartlog in any Git repo and get the same graph.

I often use this alias and add additional flags.

$ git smartlog --since="2020-05-01"

Save Points

Commit often in small spurts to save your work and rebase later.

$ git config --global "!git add -A && git commit -m 'SAVEPOINT'"

You can now type git save, and all your changes will be committed with the message “SAVEPOINT”.


Please rebase your commits later on, so you don’t have a bunch of “SAVEPOINT” commits.

Phil and Amy might shame you if they get too many “SAVE POINT” BitBucket notifications.

Amend Your Last Commit

Sometimes you make a commit and realize moments later that you left a console.log or forgot to commit a file.

$ git config --global alias.amend="commit -a --amend"

You can now type git amend, and all your changes will be added to the last commit. You will also have a chance to change the commit message.

Disclaimer: Don’t run git amend if you already pushed.

Applying Changes Local Onto Changes From Upstream

You are happily committing changes on your local and goto push but you’re stopped. Someone committed to the branch upstream. You can do git pull and fix the merge conflict OR you can do this.

$ git config --global alias.up=!git pull --rebase --prune $@

You can now run git up and the changes from upstream will be pulled and local commits will appear at the end of the history. The commit history now is a straight line.

Learn More

One More Thing

Did you know you can commit “hunks” and not just files using the command line.

Run git add -p. This command opens an interactive experience that takes you through each file, and you can say whether you want or don’t want something to be added to the stage.

comments powered by Disqus