Reviewing each line of code with Git
"Every line of code, every comment, every line-break should have a purpose."
We usually use Github's review feature for code reviews. It is hard, however, to jump between files when the changes are large. An ideal way would be to manually mark each and every line as checked during the review.
I use a combination of git reset and git add -p to do just that. Sharing the recipe below 🤫.
- Switch to the branch we want to review.
- Merge main into it.
- Create a new review-branch from that branch.
- Git reset to unstage differences from main.
- Use patch mode to break changes into small pieces.
- Add and stage reviewed lines. This is where we manually mark each and every line we have reviewed. We can also make changes as we review.
- Go back to the source branch we started the review with.
- Checkout changes from our review-branch to this branch.
- Push changes from our review.
The whole process looks something like this.
# create a new review branch git switch branch-to-review git merge main git checkout -b review-branch-to-review # unstage changes from main git reset main # review each and every line using patch mode git add . -p # make edits during review # work as we normally do git commit -m "<message about changes>" # go back to the branch-to-review git checkout branch-to-review # stage changes from our review branch # the dot (.) tells to checkout the changes in current directory # --no-overlay tells to include deleted files git checkout branch-to-review . --no-overlay git commit -m "<review message>"
Automating the above using Justfile
These are still a lot of commands. It is easy to mess them up. Wouldn't it be great if we can do something like:
just review # work on code as we normally do # and then just finish-review