Here's a Git command I've started to use a lot:

git rebase -i origin/master

In fact, I use it often enough that I made a shell alias:

alias grb = 'git rebase -i origin/master'

What does it do? It lists all the commits I made locally but didn't push to the remote master branch yet.

pick 3b052b2 New blog post: Interactive rebase against the remote master

pick 2b05fe3 Work work

pick d37dda8 More work































The list opens in my editor as a Git "interactive rebase", which means I can easily modify the list to rephrase commits, reorder them, and squash multiple commits into one.

If you're an experienced Git user, you have probably used interactive rebase before. But what makes this command so handy is how you don't have to dig through your git log and figure out which commit to rebase from. It changes interactive rebase from something you spend maybe 30 seconds on to something you spend maybe 5 seconds on.

If you think about it, the commits you haven't pushed yet are usually exactly the ones you want. You don't want to rewrite what's already been pushed. You may not want to change every unpushed commit, but you can change some and leave the rest as-is.

You can learn all about interactive rebase elsewhere, but here's one thing I do often, as an example:

Say I made a commit for feature A, then one for feature B. Then I realise I wanted to get some more stuff into the first commit.

I just make those changes as a third commit with a temporary commit message.

abc123 Implement feature A

def456 Implement feature B

789cba Temp

Then I open the interactive rebase with my grb alias. I move that temporary commit below "feature A" and say to "fixup" the commit – to meld it into the preceding commit, discarding the temporary commit message.

pick abc123 Implement feature A

f 789cba Temp

pick def456 Implement feature B

When I save and close this window (a quick ZZ in Vim), it's all done.

123bca Implement feature A

465efd Implement feature B

There are other ways to achieve the same (e.g. with the edit command and no third commit), but this is how I like to do it.