preface
The previous section covered branch creation, deletion, and switching. This section is about branch merging.
The merge command
When we merge code, we often encounter two situations
- There were no conflicts, and Git automatically resolved all of them for us
- There are conflicts, and Git helps us resolve some of them, but not others
So what does Git actually do in the merge process? How is it done?
use
Git merge <localBranch> <otherLocalBranch>Copy the code
How does Git automatically merge conflicts
Git resolves all conflicts
The first two branches are app.txt under branch A
he is program
I am useless
Fit
Copy the code
App.txt in branch B
I'm program
I am useless
Git
Copy the code
Git merge git merge git merge git merge git merge Do you find yourself helpless? What if I told you that both branches come from this version? App.txt for master branch
I'm program
I am useless
Fit
Copy the code
Git does this internally as well. When Git automatically merges all conflicts, Git automatically generates a commit record with information like this: Merge branch into
Git cannot resolve all conflicts
When Git encounters a conflict that it cannot resolve on its own, it displays the location of the conflict on the console. What does Git do?
- Git cannot create a commit at this point. Git usually creates a commit automatically after a merge conflict, and in the event of a conflict, you have to resolve the issue before you receive the commit
- Adds the changes for the conflict-free part to the staging area
- Insert a conflict flag in the part of the conflict that cannot be resolved, i.e
<<<<<<<HEAD
When does this conflict occur? A: This happens when different changes are made to the same part of the same file, requiring us to manually merge conflicts based on the actual situation.
Merge tips
When we encounter this situation, we can do the following quick actions when we want to use all of our own (or someone else’s) code
# re-set git checkout --ours <file>Copy the code
conclusion
Merge The merge command is one of the most commonly used commands, but most of us do not know how to use it internally. This chapter explains how to use it and how to do it internally. Let’s take a closer look at the merge command.
Past the Git
“03” Git add, branch, stash, and checkout command “02” Git