Bird
Raised Fist0
Gitdevops~7 mins

Three-way merge in Git - Commands & Configuration

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
Introduction
When multiple people change the same file in different ways, Git uses a three-way merge to combine those changes. It compares the common starting point and the two changed versions to create a final merged file.
When two developers edit the same file on different branches and you want to combine their work.
When you pull changes from a remote repository that conflict with your local changes.
When you want to merge a feature branch back into the main branch after independent work.
When resolving conflicts after rebasing your branch onto another branch.
When Git cannot automatically merge changes and asks you to fix conflicts manually.
Commands
Create and switch to a new branch called feature-branch to work on changes separately.
Terminal
git checkout -b feature-branch
Expected OutputExpected
Switched to a new branch 'feature-branch'
Commit your changes in the feature branch with a message describing the update.
Terminal
git commit -am "Change in feature branch"
Expected OutputExpected
[feature-branch abc1234] Change in feature branch 1 file changed, 1 insertion(+), 1 deletion(-)
-a - Automatically stage tracked files before committing
-m - Add commit message inline
Switch back to the main branch to prepare for merging the feature branch.
Terminal
git checkout main
Expected OutputExpected
Switched to branch 'main'
Merge the feature branch into main. Git uses a three-way merge comparing the common ancestor and both branches.
Terminal
git merge feature-branch
Expected OutputExpected
Updating 123abcd..abc1234 Fast-forward file.txt | 2 ++ 1 file changed, 2 insertions(+)
If there are conflicts, Git will pause the merge and ask you to fix them manually.
Terminal
git merge feature-branch
Expected OutputExpected
Auto-merging file.txt CONFLICT (content): Merge conflict in file.txt Automatic merge failed; fix conflicts and then commit the result.
Check which files have conflicts that need to be resolved before completing the merge.
Terminal
git status
Expected OutputExpected
On branch main You have unmerged paths. (fix conflicts and run "git commit") Unmerged paths: (use "git add <file>..." to mark resolution) both modified: file.txt no changes added to commit (use "git add" and/or "git commit -a")
After fixing conflicts in the file, stage the resolved file to mark it as ready.
Terminal
git add file.txt
Expected OutputExpected
No output (command runs silently)
Complete the merge by committing the resolved changes with a message.
Terminal
git commit -m "Resolve merge conflict in file.txt"
Expected OutputExpected
[main abc5678] Resolve merge conflict in file.txt
-m - Add commit message inline
Key Concept

If you remember nothing else from this pattern, remember: a three-way merge uses the common ancestor and both changed versions to combine edits safely.

Common Mistakes
Ignoring merge conflicts and trying to commit without resolving them.
Git will not allow committing until conflicts are fixed, causing confusion and blocking progress.
Open conflicted files, fix the conflicts, stage the files with git add, then commit.
Merging without updating the main branch first.
You might merge outdated code, causing unnecessary conflicts or overwriting newer changes.
Always pull or fetch and update your main branch before merging feature branches.
Using git merge without understanding the changes in both branches.
You may accidentally overwrite important changes or create conflicts that are hard to resolve.
Review changes with git diff or git log before merging to understand what will be combined.
Summary
Create a feature branch to work on changes separately.
Use git merge to combine branches; Git uses three-way merge to compare common ancestor and both versions.
If conflicts occur, fix them manually, stage resolved files, and commit to complete the merge.

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