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-SHA
git 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
--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.
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:
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
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:
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.
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
--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
gfa = fetch upstream && git rebase -i --autosquash upstream/master fix = git commit --fixup