Git aliases (updated)

Almost 2 years ago now I wrote about the Git aliases I was using.  And I still use most of them, but I’ve also added a few new ones worth talking about.

Backing up slightly, I’m talking about creating shortcuts to improve your workflow.  For example, I like to use the short form of git status most of the time, so I have an alias for that; git s.

To add an alias you need to edit your global config file using the following command…

git config --global alias.s status -s

Alternatively you can use a text editor and edit your global config file manually, adding the following lines…

[alias]
	s = status -s

Back when I wrote my first post on Git aliases, I was using Heroku and PHP, but it’s a while since I’ve used Heroku, so these days I have git p mapped to always push to the origin of the current branch, like this…

[alias]
	p = !git push origin $(git rev-parse --abbrev-ref HEAD)

I also find that when I return to my screen, I want a quick refresher of what I was working on last, so I have git l mapped to show the last log message…

[alias]
	l = log -1 --oneline

After too much time spent developing in ‘master’ I’ve also trained myself to create feature branches, so I have git b mapped to create a new branch…

[alias]
	b = checkout -b

Imagine you’ve just committed your changes, then spotted an errant debug message that you want to remove, but don’t want to create a new commit. Great, just use git a to update the previous commit…

[alias]
	a = git commit --amend --no-edit

To err is to be human, and sometimes you just want to undo with git u

[alias]
	u = reset HEAD~1 --mixed

And last but not least, a hard reset (with a parachute!), using git r

[alias]
	r = !git add -A && git commit -qm 'SAVEPOINT' && git reset HEAD~1 --hard

This last one has saved me more than once, but let me give you a little more background on it, as it might not be immediately obvious.  So, you’ve made some chances, gone off down the garden path and realised that it’s the wrong way and you want to revert to a last good point, so you want to do a hard reset.  Makes sense, and you do it, wiping out your working area.  “Oh, but I’d done that one bit that was clever…. nope, it’s gone”.  Creating a commit in this way detaches it from the commit tree and it will be garbage collected in time.  But for a short while at least, you can use git reflog to get it back, which can be super handy!

You have noticed I like single character aliases, so I have to be choosey about which ones I add.  I don’t like ones where I have to spend more time thinking about the acronym than it would take to type the full command.

Here’s a summary list of my aliases (yes, alphabetised!)…

[alias]
	a = git commit --amend --no-edit
	b = checkout -b
	c = !git add -A && git commit -m
	l = log -1 --oneline
	p = !git push origin $(git rev-parse --abbrev-ref HEAD)
	r = !git add -A && git commit -qm 'SAVEPOINT' && git reset HEAD~1 --hard
	s = status -s
	u = reset HEAD~1 --mixed