Git Tip: Rebase Your Stash

When pulling, I always git pull --rebase origin master. It forces my local commits to be rebased instead of merged. I find it quite more convenient and easier to deal with when in a conflict state. It also prevents automatic commit messages like “merge branch master into master” which contaminate the tree.

What about stashes?

Let’s say you’re working on master and you want to pull new commits. Here’s what I used to do:

$ git stash --include-untracked
$ git pull --rebase origin master
$ git stash pop
# fix conflict (merge) if needed

It is perfect if you know (or hope) you won’t get any conflict, but let’s say you do. When popping your stash you’ll be in a merge state. The huge pain here is that once the conflict is resolved your stash is still there. git stash pop again and you’ll most likely end up resolving that same conflict. Once resolved you’ll need to git stash drop.

Here’s a better solution:

$ git add --all
$ git commit -m '[STASH]'
$ git pull --rebase origin master
# fix conflict (rebase) if needed
$ git reset HEAD~1

Basically I just commit all my files, pull rebase (my commit goes at the top of the tree) and non-destructively uncommit (git reset HEAD~1 erases last commit but keeps related file edits).

And that’s it! Step by step rebase of my “stash”.