Bird
Raised Fist0
Gitdevops~10 mins

Three-way merge in Git - Step-by-Step Execution

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Process Flow - Three-way merge
Start: Common Base Commit
Branch A changes
Three-way merge compares
Merge result with combined changes
Commit merge
The three-way merge compares two changed versions with their common base to combine changes into one merged result.
Execution Sample
Git
git checkout branchA
# make changes and commit

git checkout branchB
# make changes and commit

git merge branchA
This sequence merges changes from branchA into branchB using a three-way merge.
Process Table
StepActionBase VersionBranch A VersionBranch B VersionMerge ResultNotes
1Identify common base commitBase file contentBase file contentBase file contentN/AAll start from same base
2Compare base to Branch ABase file contentChanged line in ABase file contentN/ADetect changes made in Branch A
3Compare base to Branch BBase file contentBase file contentChanged line in BN/ADetect changes made in Branch B
4Merge changesBase file contentChanged line in AChanged line in BCombined changes or conflict markersIf changes overlap, conflict occurs
5Resolve conflicts if anyN/AN/AN/AFinal merged contentUser edits conflicts manually
6Commit mergeN/AN/AN/AMerge commit createdMerge is complete
💡 Merge commit created or conflicts resolved and committed
Status Tracker
VariableStartAfter Step 2After Step 3After Step 4After Step 5Final
Base file contentOriginal textOriginal textOriginal textOriginal textOriginal textOriginal text
Branch A file contentOriginal textChanged line in AChanged line in AChanged line in AChanged line in A or resolvedFinal merged content
Branch B file contentOriginal textOriginal textChanged line in BChanged line in BChanged line in B or resolvedFinal merged content
Merge resultN/AN/AN/ACombined changes or conflict markersFinal merged contentFinal merged content
Key Moments - 3 Insights
Why does git need a common base commit for merging?
Git uses the common base commit to understand what changed in each branch separately. This helps it combine changes correctly or detect conflicts, as shown in execution_table rows 1-3.
What happens if both branches change the same line differently?
Git marks a conflict in the merge result (row 4). The user must manually resolve it (row 5) before committing the merge (row 6).
Can a merge happen without conflicts?
Yes, if changes are on different lines or parts, git automatically combines them (row 4) and creates a merge commit (row 6).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 4. What does the 'Merge Result' show if both branches changed the same line differently?
AConflict markers showing both changes
BOnly Branch A changes
CCombined changes without any markers
DOnly Branch B changes
💡 Hint
Refer to execution_table row 4 under 'Merge Result' and 'Notes' columns.
At which step does the user manually resolve conflicts?
AStep 3
BStep 4
CStep 5
DStep 6
💡 Hint
Check execution_table row 5 'Action' and 'Notes' columns.
If Branch A made no changes, what would the merge result be at step 4?
AConflict markers appear
BMerge result equals Branch B version
CMerge result equals Base version
DMerge fails
💡 Hint
Look at variable_tracker for Branch A and Branch B content changes after step 4.
Concept Snapshot
Three-way merge in git:
- Uses common base commit plus two branch versions
- Compares changes from base to each branch
- Combines non-conflicting changes automatically
- Marks conflicts if same lines changed differently
- User resolves conflicts manually
- Final merge commit records combined history
Full Transcript
A three-way merge in git starts by identifying the common base commit shared by two branches. Then git compares the base version with each branch's version to find changes. If changes do not overlap, git automatically combines them into a merged result. If both branches changed the same lines differently, git marks conflicts in the merged file. The user must manually resolve these conflicts before committing the merge. The final merge commit records the combined changes and history from both branches.

Practice

(1/5)
1. What is the main purpose of a three-way merge in Git?
easy
A. To combine changes from two branches using a common base
B. To delete a branch after merging
C. To create a new branch from the current branch
D. To reset the current branch to a previous commit

Solution

  1. Step 1: Understand the concept of three-way merge

    A three-way merge uses the common ancestor of two branches to combine their changes safely.
  2. Step 2: Identify the purpose in Git workflow

    This process helps merge changes from two branches without losing work, especially when both branches have edits.
  3. Final Answer:

    To combine changes from two branches using a common base -> Option A
  4. Quick Check:

    Three-way merge = combine changes safely [OK]
Hint: Three-way merge combines two branches with a common ancestor [OK]
Common Mistakes:
  • Confusing merge with branch deletion
  • Thinking merge creates new branches
  • Mixing merge with reset commands
2. Which Git command automatically performs a three-way merge when integrating changes from a remote branch?
easy
A. git pull
B. git merge
C. git commit
D. git branch

Solution

  1. Step 1: Recall commands that fetch and merge

    git pull fetches changes from a remote branch and merges them locally, often using a three-way merge.
  2. Step 2: Differentiate from other commands

    git merge merges branches locally but does not fetch remote changes; git branch manages branches; git commit records changes.
  3. Final Answer:

    git pull -> Option A
  4. Quick Check:

    git pull = fetch + merge (three-way) [OK]
Hint: git pull fetches and merges remotely with three-way merge [OK]
Common Mistakes:
  • Choosing git merge without fetching first
  • Confusing git commit with merge
  • Using git branch for merging
3. Given the following scenario:
Branch A has file.txt with content: "Hello World"
Branch B modifies file.txt to: "Hello Git"
Common base has file.txt: "Hi World"
What will be the content of file.txt after a successful three-way merge of Branch B into Branch A?
medium
A. "Hello"
B. "Hello Git"
C. "Hello World Git"
D. "Hello World"

Solution

  1. Step 1: Identify changes from base to each branch

    Base has "Hi World". Branch A changed "Hi" to "Hello". Branch B changed "Hi" to "Hello" and "World" to "Git".
  2. Step 2: Understand three-way merge result

    Since the change "Hi" -> "Hello" is common to both branches, and Branch B has an additional change "World" -> "Git", Git's three-way merge automatically combines them, resulting in "Hello Git".
  3. Final Answer:

    "Hello Git" -> Option B
  4. Quick Check:

    Three-way merge picks combined changes, here "Hello Git" [OK]
Hint: Merged content reflects changes from both branches via base [OK]
Common Mistakes:
  • Assuming content concatenates both changes
  • Ignoring base version in merge
  • Confusing which branch's changes apply
4. You ran git merge feature but got a conflict in app.js. What should you do to resolve this three-way merge conflict?
medium
A. Run git reset --hard immediately to discard all changes
B. Delete app.js and run git merge --abort
C. Edit app.js to fix conflicts, then run git add app.js and git commit
D. Run git branch -d feature to delete the feature branch

Solution

  1. Step 1: Understand merge conflict resolution

    When a conflict occurs, you must manually edit the conflicting file to resolve differences.
  2. Step 2: Stage and commit resolved file

    After fixing conflicts in app.js, use git add to stage and then git commit to complete the merge.
  3. Final Answer:

    Edit app.js, then git add and git commit -> Option C
  4. Quick Check:

    Fix conflicts, stage, commit to resolve merge [OK]
Hint: Fix conflicts manually, then add and commit [OK]
Common Mistakes:
  • Deleting files instead of resolving conflicts
  • Aborting merge without fixing conflicts
  • Deleting branches to fix conflicts
5. You have two branches, main and feature. Both modified the same function in utils.py differently. After running git merge feature into main, a three-way merge conflict occurs. Which approach best resolves this conflict while preserving both changes?
hard
A. Delete feature branch and continue with main unchanged
B. Force merge with git merge --strategy=ours to keep main version only
C. Reset main to the common ancestor commit and retry merge
D. Manually edit utils.py to combine both changes logically, then stage and commit

Solution

  1. Step 1: Recognize conflict in same function

    Both branches changed the same function differently, causing a conflict during merge.
  2. Step 2: Choose best resolution method

    Manually editing the file to combine both changes logically preserves work from both branches, which is the best practice.
  3. Step 3: Stage and commit after resolving

    After editing, stage the file with git add and commit to complete the merge.
  4. Final Answer:

    Manually edit utils.py to combine changes, then stage and commit -> Option D
  5. Quick Check:

    Manual merge preserves both changes best [OK]
Hint: Edit conflicts to combine changes, then add and commit [OK]
Common Mistakes:
  • Using --strategy=ours loses feature changes
  • Deleting branches to avoid conflicts
  • Resetting loses recent work