Here’s a Git command I’ve started to use a lot:
In fact, I use it often enough that I made a shell alias:
What does it do? It lists all the commits I made locally but didn’t push to the remote
master branch yet.
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.
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.
When I save and close this window (a quick
ZZ in Vim), it’s all done.
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.
I work almost exclusively in the
master branch locally, pushing directly to the remote
master branch. This is very intentional and something I recommend exploring. If you prefer feature branches, I’m sure the command could be adapted to account for whatever remote branch you’re currently tracking. Feel free to share that in the comments!