1. Understand branches

In general, the branching scenarios are as follows:

  • When multiple branch code is merged into one branch
  • Multiple branches push to the same remote branch

This is when multiple branches change the same file (anywhere) or multiple branches change the name of the same file

If the two branches modify parts of different files, there is no conflict, but simply merge

Conflicts can occur in commands such as push, pull, Stash, and rebase. Essentially, conflicts occur when merge and patch are applied

Second, the analysis

Create an A.txt file at the start of the file with “master commit” as follows

It is then submitted to the warehouse:

  • git add a.txt
  • git commit -m ‘master first commit’

Create a new branch, featurel1, and switch as follows:

git checkout -b featurel1
Copy the code

Then change the a.txt file to featurel COMMIT, add it to the staging area, and start committing to the repository:

  • git add a.txt
  • git commit -m ‘featurel first change’

Git merge (git merge, git merge

At this point, the contents of the A.txt file become Featurel Commit, and there are no conflicts because git merges quickly internally

If every commit from the current branch already exists in another branch, Git performs a “fast forward” operation

Git does not create any new commits, but simply points the current branch to the merged branch

If you switch to the Featurel branch at this point, change the content of the file to Featrue Second Commit, and then commit to the local repository

Then switch to the main branch, if the a.txt file is changed again, change it to Mastet Second Commit, and commit to the local repository again

At this point, the Master branch and Feature1 branch each get a new commit, which should look something like this:

In this case, instead of performing a quick merge, you try to merge your changes together, which can be conflicting

Now branch merge with Git Merge Featurel, as follows:

As can be seen from the conflict information, A.txt has a conflict and must be manually resolved before submission

Git status can also tell us about conflicting files:

Open the a.txt file and you can see the following:

It marks the content of the different branches with <<<<<<<, =======, >>>>>>> :

  • The area between <<<<<<< and ======= is the current change
  • The area between ======= and >>>>>>> is where the changes come in

All you need to do now is make changes to the conflicting content, using the git add command for each file to mark it as resolved. Once the conflicting files are temporarily saved, Git marks them as resolved and commits:

  • git add a.txt
  • git commit -m “conflict fixed”

The Master branch and Feature1 branch now look something like this:

You can see the merge information using git log:

Third, summary

If Git cannot merge branches automatically, the conflict must be resolved first. After the conflict is resolved, the commit is completed

Resolving the conflict is to manually edit the files that Git failed to merge into the desired content and then commit