Skip to main content

FixConflict

AndyBinOriginalAbout 1 min

Conclusion

When Git can't automatically merge branches, it has to resolve conflicts first. After the conflict is resolved, commit again, and the merge is complete.

Conflict resolution is to manually edit the files that failed to be merged by Git to the content we want, and then submit them.

View the branch merge diagram:

git log --graph

Info

Conflicts generally occur in the following two situations:

  • The code of the remote repository is lagging behind the local repository
  • The code of the remote repository is far more than the local repository

When you haven't submitted the code, your colleagues have already submitted the code, which will cause the remote repository code to lead your code.

Caption

How conflict is represented

When a merge conflict occurs, the section is denoted by <<<<<<<, ======== and >>>>>>>. The part before ======== is the case of the current branch, and the part after ======== is the case of the incoming branch.

How to resolve conflicts

After seeing the conflict, you can choose between the following two ways:

  • Decided not to merge. At this point, the only thing to do is to reset the index to the HEAD node. git merge --abort is used in this case.
  • resolve conflicts. Git will mark the conflicting place. After resolving the conflicting place, use git add to add it to index, and then use git commit to generate merge nodes.

You can use the following tools to resolve conflicts:

  • Use the merge tool. git mergetool will invoke a visual merge tool to handle conflicting merges.
  • See the difference. git diff will display three-way diffs (the three-way comparison algorithm used in three-way merges).
  • See the diff for each branch. git log --merge -p <path> will show the difference between the HEAD version and the MERGE_HEAD version.
  • View the pre-merged version. git show :1:filename shows the version of the common ancestor, git show :2:filename shows the HEAD version of the current branch, git show :3:filename shows the MERGE_HEAD version of the opposite branch .
Last update:
Contributors: rumosky
Comments
  • Latest
  • Oldest
  • Hottest