Sunday, December 13, 2020

Github Pull Request Resolve Conflicts with VSCode (many conflicts)

When trying to merge a pull request and faced with hundreds of conflicts the UI option in Github will no longer be an option.. and instead you are going to have to do the merge yourself using the command line! 

Luckily vscode makes this process visual and easy to digest. It has become my preference for resolving any number of conflicts.

Lets take a look at the following scenario:



This is a simple example with only 1 conflict. As you can see from the above - developer #1 created a branch "feature/developer1" off of the main branch and began working on the NewFeature class. At the same time, developer #2 also branched off of main creating "feature/developer2" branch... and began working on the same NewFeature class.

Next in the timeline, developer #1 finished their feature and merged a pull request back into main. Then, developer #2 completed their changes and attempted to merge their pull request back to main....


Uh oh.. developer #2 is presented with "Can't automatically merge."


Now we could use the Github UI for this and easily resolve the 1 conflict - 

But let's say the number of conflicts was in the hundreds! Github will actually not allow you to use their UI at that point. Enter the command line and vscode to the rescue.

Github has a quick cheat sheet shown above if you forget, just click the view "command line instructions" link to expand it.

Now remember we are trying to merge feature/developer2 branch back into the main branch. 

So we locally checkout the feature/developer2 branch.. and then merge that back to main! Easy

After entering the command "git merge origin/main" vscode enters a visual resolve conflicts mode:

You will be presented with a list of all conflicts on the left source control menu. In this example we just have the 1 conflict to resolve. We can manually edit the file and save.. or we can click to either "Accept Current Change", "Accept Incoming Change", "Accept Both Changes" ... etc. There are several options for handling the conflict.

After getting the file in question resolved of all conflicts, save and simply push a new commit back up to the branch you are trying to merge in the Pull Request.


Now back in Github, we have a clean PR with a new commit and it's ready to be merged back into the main branch! No problem.


Happy PR merging!

No comments:

Post a Comment