Rietta.com Security logo
You are reading The Rietta Blog, a publication about the web since 2005.

Fixup Your Code Reviews With Git Rebase --autosquash

Here at Rietta, we like to do in-depth code reviews that are sometimes accompanied with feedback that may require changes to be made to the pull request. When making additional commits with changes based on the feedback, we can get into a messy workflow that can lead to complex branch wrangling.

In this article, I will go over a few Git commands to help ease our post code-review revisions: – git commit --fixup commit-SHAgit rebase -i --autosquash source-branch

Our team utilizes Github’s Squash and merge when merging into master, with semantic git commits that are specific to their respective code changes. By organizing our commits this way, code reviews feel more like a book with related content grouped together in sections.

After the initial review, the reviewer scans the PR’s new commits making the review and feedback process smoother. When responding to the feedback with changes to our PR, we could add a bunch of stacked commits and interactively rebase them or make a commit with all the fixes. While the later of the two is not as ideal, we can make the former a quicker process by automating it with some useful git commands.

git commit --fixup

Due to feedback we’ve received on our code review, we have some changes that we need to make to feature A. After adding our changes to our staging area via git add, we could just push up the commit and do an interactive rebase later to merge/squash our changes into their related commits or we can let --fixup and --autosquash take care of all that for us.

Instead of just doing a git commit -m 'Fixup for feature A' and git pushing it up, we can target the commit that we would want to merge this hunk with when we’re reading to rebase the branch.

1
git commit --fixup d8c7823
  • We’re committing a fixup commit targeting commit sha ‘d8c7823’ which is the commit for Feature A.

git rebase -i --autosquash

Great, now if we run --autosquash with our git rebase -i source_branch as git rebase -i --autosquash source_branch we’ll get:

1
2
3
pick d8c7823 Add functionality for feature A
fixup 4fdbc87 fixup! Add functionality for feature A
pick 1a3d985 Add functionality for feature B

Without having to move around any commits and changing the command from pick to fixup, Git will automatically sort them based on what we had done with the git commit --fixup commit-sha command earlier. Also notice that it added the message fixup! Add functionality for feature A to indicate to us what is happening in the interactive rebase. The -i or interactive mode we are using in our example is optional and the same functionality will execute without the interactive editor opening up.

We also have some feedback for feature B, we can add additional commits the same way as before by running git commit --fixup 1a3d985(sha for feature B commit). When we rebase it with --autosquash it will be organized as so:

1
2
3
4
pick d8c7823 Add functionality for feature A
fixup 4fdbc87 fixup! Add functionality for feature A
pick 1a3d985 Add functionality for feature B
fixup 2fc7684 fixup! Add functionality for feature B

As a last step, we can confirm our fixes and push our rebased branch to the remote repository for our reviewers to look over said changes.

Conclusion

Code reviews can result in a lot of changes needing to be made on your pull request. Those changes can be dumped into the PR as a commit with an atomic changeset or we can keep it nice and tidy with --fixup and --autosquash. You can make it even easier by making convenient git aliases so you can execute these commands on the fly, that way keeping your PR won’t be a chore but a delight!

Here are some example shortcuts you can make to your gitconfig

1
2
3
gfa = fetch upstream && git rebase -i --autosquash upstream/master

fix = git commit --fixup

About Christopher Choi

Christopher Choi's photo

Chris is a software developer on the Rietta team. He enjoys learning about new technologies and has recently taken a liking to frontend design and modern CSS practices. When he is not developing, he enjoys hanging out with his dog Ava and traveling around the world exploring new frontiers.

His public key is available at chrisgopher on keybase.

If there is a topic you would like us to cover,