Git Merge
Warning
Do not perform
pullor amergewith uncommitted changes.
mergeis designed to combine committed changes. You can lose work.
How Does Merge Work
E is the last shared commit.
Merge will play back the commits on both branches. If neither set of commits touches the same parts of the file, the branches are merged, and HEAD moves to the end.
gitGraph commit id: "D" commit id: "E" branch feature checkout feature commit id: "A" commit id: "B" commit id: "MERGE_HEAD → C" checkout main commit id: "F" commit id: "HEAD → G"
After the merge
gitGraph commit id: "D" commit id: "E" branch feature checkout feature commit id: "A" commit id: "B" commit id: "C" checkout main commit id: "F" commit id: "G" merge feature id: "HEAD → H"
Merge Types
Fast Forward
The default merge is called fast forward, or FF.
FF can be used when there are no local changes or local commits.
The repo was cloned previously.
Before fetch.
gitGraph commit id: "A" commit id: "B" commit id: "HEAD → C"
After fetch.
gitGraph commit id: "A" commit id: "B" commit id: "HEAD → C" commit id: "D" commit id: "E"
After merge.
git merge origin/main
gitGraph commit id: "A" commit id: "B" commit id: "C" commit id: "D" commit id: "HEAD → E"
Pull
pull combines fetch and merge. pull assumes a fast forward merge.
Before pull.
gitGraph commit id: "A" commit id: "B" commit id: "HEAD → C"
After pull.
gitGraph commit id: "A" commit id: "B" commit id: "C" commit id: "D" commit id: "HEAD → E"
True Merge
This happens trying to update the part of a file, someone else has already updated.
-
HEADpointer stays the same -
MERGE_HEADpointer on the branch to be merged. -
What can be merged cleanly is merged.
-
Index records three versions of the files.
Ancestor,HEADandMERGE_HEAD- Files are merged in the working directory with conflict markers
<<<<<<,=======, >>>>>>`
- Files are merged in the working directory with conflict markers
-
A ref called
AUTO_MERGEgets created.
Conflict Resolution
After the merge, Git will say what files need to be resolved. This is called Conflict Resolution.
To resolve:
- Open the file.
- Find the Markers.
- Delete lines you don’t want.
- Delete the markers.
Two way Conflict
In Conflict.
Here are lines that are either unchanged from the common
ancestor, or cleanly resolved because only one side changed,
or cleanly resolved because both sides changed the same way.
<<<<<<< yours:sample.txt
Your change ┌─── Conflict resolution is hard;
└─── let's go shopping.
=======
Theirs ─── Git makes conflict resolution easy.
>>>>>>> theirs:sample.txt
And here is another line that is cleanly resolved or unmodified.
Resolved, kept local change.
Here are lines that are either unchanged from the common
ancestor, or cleanly resolved because only one side changed,
or cleanly resolved because both sides changed the same way.
Your change ┌─── Conflict resolution is hard;
└─── let's go shopping.
And here is another line that is cleanly resolved or unmodified.
Three Way Conflict
zdiff3 shows the conflict with the original text, adding the ||||||| marker.
In Conflict
Here are lines that are either unchanged from the common
ancestor, or cleanly resolved because only one side changed,
or cleanly resolved because both sides changed the same way.
<<<<<<< yours:sample.txt
Yours ┌── Conflict resolution is hard;
└── let's go shopping.
||||||| base:sample.txt
Original ┌── or cleanly resolved because both sides changed identically.
(Ancestor) └── Conflict resolution is hard.
=======
Theirs ── Git makes conflict resolution easy.
>>>>>>> theirs:sample.txt
And here is another line that is cleanly resolved or unmodified.
Resolved, kept their lines.
Here are lines that are either unchanged from the common
ancestor, or cleanly resolved because only one side changed,
or cleanly resolved because both sides changed the same way.
Theirs ── Git makes conflict resolution easy.
And here is another line that is cleanly resolved or unmodified.
Conflicts Resolved
This will also check the merge status.
git merge --continue
Abort A Merge
merge --abort