DEV Community

Yuan-Hsi Lee
Yuan-Hsi Lee

Posted on

Resolving Conflicts

This week we had practices of git merge. The lab is designed to generate conflicts and we are supposed to solve it.

I created 2 issues and work on them in different branches. The first one is Add JSON format output and the second one is Add --good and --bad filter. I'm supposed to create 2 feature branches based on these 2 new features before I start working on them. However, I forgot to do so. I edited the project and did the commit before creating the branches. The master branch was pointing to this commit and I couldn't merge the feature branch to master since the master also has the same commit.

The issue here is the master and the newly created branch both point to the same commit, therefore, there is no need to commit since their contents are the same. The difference is I pushed the commit in the feature branch but did not push it to the master branch.

I could do push in the master branch since these 2 branches are technically merged when they were pointing to the same commit. But, what about the second feature and its branch? I'm supposed to merge the second one and solve the foreseen conflicts.

The solution is to create the second feature branch and point to the previous commit where the master branch was supposed to be. The command would be git checkout -b <newBranchName> <commitNumber>. In that case, my newly created feature branch would have the old content before I made any changes about adding these 2 new features.

After solving this issue, I worked on adding the new feature and then pushed it remotely. After that, I started the merging. This time, there were supposed to be several conflicts between the master branch and the second feature branch since the master branch already had the changes from the first feature branch.

I use the UI in Visual Studio Code to resolve the conflicts. There are different options such as "keep the upcoming", "keep the current", or "keep both". But, these options don't always do exactly what you expect. Programmers still need to modify the code to resolve the conflicts.

In conclusion, this practical experience of git merge gave me more than I expected. I did git merging before when I was doing an internship using Visual Studio. It was not that difficult since Visual Studio provides almost everything about version control in GUI. In this lab practice, I faced some unexpected issues which help me learn more about git.

Top comments (0)