Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Git Merge

Warning

Do not perform pull or a merge with uncommitted changes.

merge is 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.

  1. HEAD pointer stays the same

  2. MERGE_HEAD pointer on the branch to be merged.

  3. What can be merged cleanly is merged.

  4. Index records three versions of the files. Ancestor, HEAD and MERGE_HEAD

    • Files are merged in the working directory with conflict markers <<<<<<, =======, >>>>>>`
  5. A ref called AUTO_MERGE gets created.

Conflict Resolution

After the merge, Git will say what files need to be resolved. This is called Conflict Resolution.

To resolve:

  1. Open the file.
  2. Find the Markers.
  3. Delete lines you don’t want.
  4. 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

References

Git - git-merge Documentation

Last Modified • Sunday, June 14, 2026. 5:09 am UTC+00:00 • Commit: 4a9f867