Photo for 2014-03-31


Smashing Squashing Git Commits for Open Source Contributions

Recently, I made my first contribution to an open source project, homebrew-cask, “a CLI workflow for the administration of Mac applications distributed as binaries”. Basically, it’s a way to install applications through the command line, rather than visiting a website, clicking to save a disk image, opening said disk image, and clicking and dragging the application icon to the application folder. TIme-consuming and full of opportunities for distraction.

…the three great virtues of a programmer: laziness, impatience, and hubris. – Larry Wall

As I was comparing the available applications [or ‘casks’], I noticed that WireOver was missing. Now, it’s not that I use it frequently, but it’s a handy utility that’s just getting started. I checked out the contribution page – bonus: it’s Ruby – and saw how simple it was to add a cask. So, I put one together for WireOver and submitted my pull request. Because I failed to RTFM in full detail, I had a few changes to make for a few lines of code. Which, of course, meant a few commits. The cask audit passed and I thought all was good.

And it sort of was. A request came in this morning to squash my commits. I had previously gone through the link provided in the docs for squashing commits, but ran into roadblocks; specifically, I wasn’t getting my commit messages to show up, only the word “noop” in the text file that pops up. 

I found another article, this time geared toward open source contributors, which helped more, but didn’t quite get me there. Here’s what I ended up doing after I had already pushing my feature branch to my fork of the project.

  1. From the command line, get to your branch in the project repo. 
  2. Run git log to see all of your most recent commits. Make note of how many you want to combine. 
  3. Hit q to exit the log.
  4. Back to the command line: type git rebase -i BRANCH_NAME~NUMBER_OF_COMMITS. As an example, with less yelling: git rebase -i wireover~5
  5. This command will open up your default text editor, showing the five most recent commit messages and their hashes. Below them are some fine directions. Basically, you’ll change all but one of the pick to squash. To double-check my work, I had another terminal window open with the results of git log so I could check the full messages against the hashes in the text editor. 
  6. Make changes, save, and close. 
  7. Run git log again to make sure all of the commit messages are mashed into one.
  8. Once that all looks good, run git push -f GITHUB_USERNAME BRANCH_NAME. Or, once again with less yelling, git push -f chrisbodhi wireover. This is dicey because you’re telling git to force the changes. If you don’t though, you’ll get an error message that tells you that your local branch is now somehow behind your remote branch and just go ahead and pull it down and ruin your squashing. But it’s a trap. Don’t do it. If anyone knows of a better way, feel free to hit me up on the bird thing
  9. And that should be it! The commit message of your earlier pull request should be updated with the squashed message. Now it’s time to kick back and reap the rewards of contributing to open source software in an easy-to-read and helpful manner.

[comic src]