How I make WIP git commits painless
I’m a huge fan of WIP (work-in-progress) commits for pressing pause, so to speak, in the middle of work on a fix or feature. It’s not uncommon that I’ll need to context switch in the middle of work on some feature to help someone else on my team out with checking something, because it’s the end of my work day/session and I don’t have a clean set of changes to commit, etc. Sometimes git stash
can be useful for this, especially if I know it’s a quick context switch and I’ll be right back to what I was working on. However, sometimes using stash can get messy, or just confusing, and so I avoid that if there are a lot of changes.
Instead, I like to use WIP commits in these situations, where I basically do git add -A && git commit -m'WIP'
. This adds all unstaged changes, and commits them with the commit message WIP
. But as much as I like using WIP commits for placeholders/pausing work, I hate commit messages that aren’t descriptive. There’s nothing worse than using git blame
to try and figure out why some change was made and seeing a two year old commit with tons of changes with the message “WIP”.
So instead, I always undo my WIP commits when I return to whatever I was working on. To do that, I use git reset "HEAD^"
. To make things easier, I have these two aliases set up – note that these work for me with zsh as my shell; you may need to tweak them if you use bash, fish, etc.
alias wip="git add -A && git commit -m'WIP'"
alias guncommit='git reset "HEAD^"'
The problem with WIP commits, though, is that you might return to work on a particular branch a few minutes later… or a few hours, days, or weeks later. In some cases, you might forget that you had some work in progress, and forget that you made a WIP commit. To solve this problem, in my zsh prompt, I have a function that checks for the existence of a WIP commit, and if it exists, makes that very obvious in my terminal:
wip=$(git log -1 --pretty=%B)
if [[ $wip = "wip" || $wip = "WIP" ]]; then
prompt_segment red black
echo -n "<wip>"
fi
This code checks if the most recent commit message was wip
or WIP
, and if so, outputs a prompt segment in red with black text that simply says <WIP>
. This is unmissable in my terminal: