DEV Community

loading...
Cover image for Conflict resolved

Conflict resolved

yzwdroid profile image Andy Yang ・2 min read

How everything starts

For this week's lab(3), I practiced merging with conflict. I chose to add two features on my goURL program. Then, I filed two issues on Github.

Make two branches

Before this lab, all the coding happens on the master branch. So I made two more branches. These two branches all based on the current state of master.

git branch issue-5
git branch issue-6
Enter fullscreen mode Exit fullscreen mode

Fast-forward merge

Then I started work on issue-5 branch git checkout issue-5. After I finished all the coding git commit -m "issue-5 finished". Then, I merged this branch to master

git checkout master
git merge
Enter fullscreen mode Exit fullscreen mode

This is a Fast-forward merge. Because the commit pointed to by the branch issue-5 was directly ahead of the commit on the master branch.

Conflict happens

After the Fast merge, I started work on issue-6 git checkout issue-6. The coding part goes smoothly.

git commit -m "issue-6 finished"
git checkout master
git merge issue-6
Enter fullscreen mode Exit fullscreen mode

Automatic merge failed.

# aborting a merge
git merge --abort
Enter fullscreen mode Exit fullscreen mode

Resolving a merge conflict

Git adds standard conflict-resolution markers to the files that have conflicts.

<<<<<<< HEAD:index.html
aaaaaaa
=======
bbbbbbbbbbb
>>>>>>> issu-6:index.html
Enter fullscreen mode Exit fullscreen mode

After you’ve resolved each of these sections in each conflicted file, run git add on each file to mark it as resolved. Staging the file marks it as resolved in Git.

Advanced merge

In my situation, too many markers on my main.go. So I found another way to solve the conflict. I am a vim user, and git can specify the mergetool.

git config --global merge.tool vimdiff  
Enter fullscreen mode Exit fullscreen mode

After I run git mergetool, vimdiff was executed.

Screenshot 2020-10-10 3_23_31 PM

Local changes (master branch in this case) are in the upper-left window, followed by a closest common ancestor and the issue-6 branch in the upper-right corner. The result of the merge is in the bottom window.

  • LOCAL: This is the file from the current branch (or whatever you're merging into)
  • BASE: The common ancestor—how the file looked before both changes took place
  • REMOTE: The file you are merging from another branch (issue-6)
  • MERGED: The merge result—this is what gets saved as output

Inside vim, :diffget REMOTE or :diffg R to get the REMOTE version. Same as :diffget BASE :diffget LOCAL. ]c [c move by changes. Repeat for every conflict and then run :wqa

Commit the merge result

git commit -m "issue 5 and issue 6 conflict resolved"
Enter fullscreen mode Exit fullscreen mode

Conclusion

It's a long and fun journey to create and deal with the conflict. It simulates the methods when we all work parallel in the opensource world.

Discussion (0)

pic
Editor guide