I shared yesterday about branch merging, which is often accompanied by conflicts. Today’s post will explain how to resolve this conflict.

4 Resolve conflicts when merging branches

4.1 Preparing the Presentation environment

  1. Use commands in empty foldersgit initInitialize an empty warehouse
  2. Using the commandecho "hello" >> README.mdcreateREADME.mdfile
  3. Using the commandGit add. && git commit -mCommit the changes. Note that no command is used hereGit commit -amBecause theREADME.mdThe file is untraceable
  4. Using the commandgit checkout -b exampleCreate and switchexamplebranch
  5. Using the commandecho "hello, example" >> a.txtCreate a file and write a paragraph in it
  6. Using the commandGit add. && git commit -m "example branch change"Commit changes
  7. Using the commandgit checkout masterToggle values for the main branch
  8. Using the commandecho "hello, master" >> a.txtCreate a file and write a paragraph in it
  9. Using commands CommandsGit add. && git commit -mCommit changes

4.2 Viewing Details about merge Conflicts

In the previous demo environment, both the master and Example branches modify the A.txt file, and both are the first line. In Git, merge conflicts occur if both branches modify the same file and part of it. Merging the example branch with the Master branch gives the following effect:

$ git merge example
CONFLICT (add/add): Merge conflict in a.txt
Auto-merging a.txt
Automatic merge failed; fix conflicts and then commit the result.
Copy the code

The output above only tells us that the merge was unsuccessful and lists which files failed to be merged. For more details, use the git status command:

$ git status
On branch master
You have unmerged paths.
  (fix conflicts and run "git commit")
  (use "git merge --abort" to abort the merge)

Unmerged paths:
  (use "git add <file>..." to mark resolution)
	both added:      a.txt

no changes added to commit (use "git add" and/or "git commit -a")
Copy the code

Here we can see that there are some unmerged files on the master branch and whether they have been modified on both branches.

If you look at the contents of the a.txt file at this point, you will actually see some changes from the file before the merge operation.

$ cat a.txt
<<<<<<< HEAD
hello, master
=======
hello, example
>>>>>>> example
Copy the code

Among them, the < < < < < < < between HEAD and = = = = = = = the contents of the said modifications on the current branch, = = = = = = = with > > > > > > > content between example said example branch modify the content.

4.3 Simple solution of merge conflict

Git commit-am: Git commit-am: Git commit-am: Git commit-am: Git commit-am: Git commit-am

Git mergeTool provides a graphical tool to resolve merge conflicts by default. The command is Git mergetool, which has been installed and configured in advance.

$ git mergetool
Merging:
a.txt

Normal merge conflict for 'a.txt':
  {local}: created file
  {remote}: created file
Couldn't set the locale: unsupported locale setting; falling back to 'C'LOCALE (MELd :9868): gTK-warning **: 23:06:25.520: Locale not supported by C library. Using the fallback'C' locale.
Copy the code

In the merge tool, resolve merge conflicts until you are satisfied. When you exit the merge tool, Git will ask if the merge is complete, and if yes, commit the merged file to the staging area, and then commit the merged file to the repository using Git Commit.

4.4. Resolve complex merge conflicts

Section 4.3 shows that, based on the status of file conflicts described in Section 4.2, you can use some graphical Git tools or file editors to modify merge conflicts.

However, in the process of project development, there will often be complex merge conflict situations, in which case, the above method will be a bit of trouble to solve, in the process of solving, you need to use some high-level Git commands, there are mainly the following 5 cases, the detailed content is not introduced here.

  • Abort merge, related command:git merge --abort
  • Ignoring whitespace characters, related commands:git merge -Xignore-space-between whitespace
  • Manual file processing and submission, related commands:git show :1:<file_name> >> <new_file_name>
  • Conflict detected, related command:git checkout --conflict=diff3 <file_name>
  • Merge logs, related commands:git log --online --left-right --merge
  • Combined differential format, related commands:git diffEtc.