Four Useful Git Aliases

Git logo

As you may know, Git is not a single application, but rather a toolkit containing many small programs and scripts that can manipulate the repository. This makes it trivial to chain those components into more-powerful, custom commands which can be defined as git aliases.

Here are some of the more-useful aliases I use on a daily basis:

(To install these, simply drop copypaste the lines above into your ~/.gitconfig file)

git p

p = "!git pull --ff-only"

This is a simple alias which performs a git pull --ff-only. I can run git p in any repository to ensure I always have the latest version of that branch without accidentally creating extra merge commits.

gix fixup <commit>

fixup = !sh -c 'REV=$(git rev-parse $1) && git commit --fixup $@ && git rebase -i --autostash --autosquash $REV^' -

This one is probably my favorite - it automates the process of amending any previous commit with the currently-staged changes. For example, if I need to amend the commit before the current one, I can simply run something like this:

vi somefile.ext
git add somefile.ext
git fixup HEAD^

Any commit or "tree-ish" should work for the argument: HEAD~2, abcd123, feature/foobar, etc.

This solution is much more elegant than my old git amend-old script which worked fine but was overly complicated.

git cleanup

cleanup = "!git branch --merged | grep -v -P '^\\*|master|develop' | xargs -n1 -r git branch -d"

This command will delete all local branches which have been merged into the current branch, except those named master and develop which it'll never delete. It's an easy way to keep your local repo clean.


git children

children = "!bash -c 'c=${1:-HEAD}; set -- $(git rev-list --all --not \"$c\"^@ --children | grep $(git rev-parse \"$c\") ); shift; echo $1' -"

Git commits are directly tied to their parents, making it easy to navigate backwards through the history. But what if you've checked out some random commit and want to move forwards instead? git children will identify all known commits which have the current HEAD as their parent!

Other Aliases

What custom git aliases are you using? Let me know in the comments!

About Colin O'Dell

Colin O'Dell

Colin O'Dell is a Senior Software Engineer at SeatGeek. In addition to being an active member of the PHP League and maintainer of the league/commonmark project, Colin is also a PHP docs contributor, conference speaker, and author of the PHP 7 Migration Guide.