splitting a big pull request into smaller, review-able ones

Have you ever submitted a pull request(PR) but it got rejected straight away because it’s too big? Well I did and you’ve got to give it to the reviewers, huge PRs are a pain in the ass to review. There are various reasons why PRs should be short and concise but I will not cover that in this blog. Instead I will share with you how I managed to split a big PR (it was some 2500+ new lines of code) into smaller ones (I ended up with some 7 PRs).

Let’s say that the project we’re working on has it’s main branch master, and a new feature branch was created based on that and we’re going to name it feature/cool_new_feature_branch. We then created a new PR, called it some_huge_pr, submitted it and it is now pending and active since you’re told that you need to break it down.

In my particular scenario I was working on an new web service and it also depended on other web services, where I also created some endpoints or done some changes, and therefore for me it was easy to break it down by feature (or web services in my case). Find a sensible way how to break down your work and plan ahead how you are going to split your big PR. Find common grounds and create small groups of code implementations.

Step 1

First thing we want to do is compare the differences between master and feature/cool_new_feature_branch. In your Git terminal type this command.


git diff master feature/cool_new_feature_branch > ../huge_pr_patch_file

view raw

splitprone.txt

hosted with ❤ by GitHub

This command compares all the changes between master and feature/cool_new_feature_branch and creates a new file called huge_pr_patch_file in the root directory of your current Git connection. If you had to actually open this file you’ll find the code you wrote, in my case C#, with added Git syntax.

Step 2

Let’s switch back to master branch and create a new branch that will have just a portion of the new code. This will eventually be the new PR so we need to start thinking per feature here.


git checkout master
git checkout -b feature/first_small_feature_branch

view raw

splitprtwo.txt

hosted with ❤ by GitHub

Step 3

Whilst checked in on the new branch run the following Git command to apply all the code changes from feature/cool_new_feature_branch to our new branch feature/first_small_feature_branch.


git apply ../huge_pr_patch_file

You should now be able to see a lot of changes in your new branch. From here stage any files you want, commit and push them. For this particular step I like to use Visual Studio’s Team Explorer tools. It displays all the files in a tree structure, and you can use Visual Studio’s file comparison feature. Then again, this is just my personal preference.

Step 4

After committing the staged files, we can stash all the remaining changes (that we will not commit in this branch) including any untracked files. From here proceed to open a new pull request as you normally would.


git stash –include-untracked –keep-index

view raw

splitprfour.txt

hosted with ❤ by GitHub

Step 5

Repeat steps 3 and 4 until all the code is committed and submitted into new smaller PRs. When splitting PRs be sure to include any code dependency and/or references so that the code compiles. If, when splitting, you notice that some of your code relies on code that is in another branch then create the new branch from that one. If not create the new branch from master.


git checkout master
git checkout -b feature/second_small_feature_branch

view raw

splitprfive.txt

hosted with ❤ by GitHub

In conclusion, I’m not going to lie, I’ve only used this approach once (just last week) and I’m no Git expert. Is it possible that there are more efficient methods or even tools that do the job? Yes, I wouldn’t be surprised but this method worked for me and next time I’ll face the same issue I will try to use this method again.

I hope it’s useful for you too and if you run into any issues feel free to drop me a message to fix this post. The one thing worse than not finding a solution online, is finding a solution that doesn’t work!

Until next one,
Bjorn

reconciling your workspace in visual studio 2017 after a gated check-in

An interesting feature of Azure DevOps (formerly known as Team Foundation Server) is the so called gated check-in. What is a gated check-in? In simple terms, every time a developer commits some code a build machine will try to compile this shelveset before merging it into the current code repository. This feature is available to both Azure DevOps Service, which is Microsoft’s cloud service, and Azure DevOps Server, which is the exact same thing but on-premises.

As a developer you’d realise that the gated check-in feature is on because when you try to commit your changes you get prompted with a window similar to this.
1

We’re OK with it so we’re going to go ahead and click on Build Changes, and then after that we can notice a notification in the Team Explorer window.2

I like to click on the link where it says here. This opens a new window (see below) in Visual Studio. I tend to keep that open until the build agent is ready and the gated check-in is complete.
3.JPG

Clicking on the View Build Details link would open a new window in your internet browser and from there you can see all the processes of the build agent. A successful check-in is evident as the text and icons would turn green in both the internet browser and Visual Studio. For that same reason I like to keep the Build Request window open in Visual Studio as we get the option to reconcile workspace straight away, screenshot below. This action will basically remove all the new checked-in files from the Pending Changes section in Team Explorer.5

In case you closed the window after a successful check-in, or never really opened it, another way you can reconcile your workspace is from the Builds section in Team Explorer. Find your most recent successful build, right click and reconcile.
6

That should be it for today, till next post,
Bjorn