Reviewing a Merge Request from the terminal

Lately, I have been reviewing Merge Requests with slow (or none at all) internet connections. Because of that, I have started reviewing MRs from my terminal, instead of using Gitlab/Github's web UI. It is faster for me, as I don't need to make requests each time I need to see what changed in each commit.

Here is the workflow I follow when reviewing a MR from my terminal

1. Project high level overview

Reviewing a MR is just a matter of seeing the changes in a branch. My first step is usually seeing where this branch started, how many commits it has and whether it is really behind the target branch or not.

For this, I run git lg. It displays the complete project history as a graph.

"git lg" output. The MR is trying to merge HEAD into master.
"git lg" output. The MR is trying to merge HEAD into master.

git lg is just an alias for a formatted git log. You can alias it by running:

git config --global alias.lg "log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit --all"

Note: Credits to the author at coderwall.com. I just take credit for adding the --all flag, so that I can see all the branches.

2. Merge Request high level overview

After seeing how this MR relates to the project, I would continue by seeing a summary of what was done in each commit. I would look at each commit's name, description and files changed.

For this, I run:

git log target-branch..feature-branch --name-status --reverse --relative-date

  • target-branch..feature-branch will print all the commits that are in feature-branch but not in target-branch.
  • --name-status will list the files that were changed in each commit, with a description of the change (renamed, deleted, added).
  • --reverse will put the oldest commit at the top and the newest commit at the bottom.
  • --relative-date will print how long ago a commit was made (like hours, days, months). Useful for teams in different timezones.

These options will print us something like this:

Files modified (M) or added (A) in each commit.
Files modified (M) or added (A) in each commit.

3. Commit detailed review

Once that I know which files where modified and in which order, I would jump into each commit detail. For that, I use:

git log target-branch..feature-branch --reverse --relative-date --histogram -U20 --color-moved

  • --histogram Okey I don't have a good reason for using it. It is just another diff algorithm that I suspect gives better results but it takes slightly longer.
  • -U20 Print the previous and the next 20 lines of each changed line. Useful for having more context when reviewing small changes in large code sections.
  • --color-moved Display with different colors the blocks of code that were just moved and not changed.

This gives us

Lines of code modified in each file for each commit.
Lines of code modified in each file for each commit.

4. Merge request diff

Just as a final review, I would generate a diff of the entire MR. For that, I use:

git diff target-branch..feature-branch --histogram -U20 --color-moved

This would produce a complete diff of the merge request

Difference between the target branch and the feature branch. Note that the lines that were only moved are highlighted with different colors.
Difference between the target branch and the feature branch. Note that the lines that were only moved are highlighted with different colors.

This view is useful when a file was changed through multiple commits.