git: rebase origin

2020-10-12

 | 

~2 min read

 | 

303 words

At Olo, we rebase our commits rather than merge to keep a cleaner git history. As with all things, there are some tradeoffs to this approach. That said, I’ve grown to really like rebasing and will reach for it before merging at this point.

One common pitfall, however, is what happens when the branch you’re looking for has been updated in a remote repository. Before pushing, you want to make sure that you have all of the most recent commits - particularly since the rebase will nearly always necessitate a force (or preferably, force-with-lease) push.

Before publishing my commits then, I find myself repeating the same steps:

  1. Stash any changes
  2. Fetch the upstream changes published to the remote (if any)
  3. Rebase on top of those changes
  4. Pop my changes

Today, I did something about it and wrote a small function in zsh. I named it gro for git rebase origin

.zshrc
function gro(){
    echo "Stashing changes"
    git stash push --all --message "stashing to rebase";
    git fetch;
    git rebase origin/$(gbc);
    echo "Popping stashed changes"
    git stash pop
}

This takes advantage of another alias I have gbc:

.zshrc
alias gbc='git branch --show-current'

Of course, this could be made explicit if you didn’t want the alias.

I’ve already used gro a handful of times in the first few hours of having it and each time I get to smile. It’s only a few seconds and keystrokes saved, but I saved those seconds and keystrokes by automating out a repeated task!

I know there’s more to do, more tasks I can automate. But that’s part of the fun - identifying them and then writing the scripts to simplify my life. It’s part of my ongoing effort to adopt what Thomas A. Limoncelli calls the A.B.A maxim or “Always Be Automating”.


Hi there and thanks for reading! My name's Stephen. I live in Chicago with my wife, Kate, and dog, Finn. Want more? See about and get in touch!