Bird
Raised Fist0
Gitdevops~10 mins

Why merging combines work in Git - Visual Breakdown

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 - Why merging combines work
Start: Two branches
Make changes on branch A
Commit changes on A
Merge branch B into A
Combine changes from both
Resolve conflicts if any
Final merged commit
Merging takes changes from two branches and combines them into one, preserving work from both sides.
Execution Sample
Git
git checkout main
# On main branch
# Make change and commit

git checkout feature
# On feature branch
# Make change and commit

git checkout main
git merge feature
This sequence switches branches, makes changes, commits them, then merges feature branch into main.
Process Table
StepActionBranchChanges PresentResult
1Checkout mainmainNo new changesOn main branch
2Make change and commitmainChange A committedMain branch updated
3Checkout featurefeatureNo new changesOn feature branch
4Make change and commitfeatureChange B committedFeature branch updated
5Checkout mainmainChange A committedBack on main branch
6Merge feature into mainmainChanges A and B combinedMerged commit created
7Check for conflictsmainNo conflictsMerge successful
💡 Merge completes combining changes from both branches into main.
Status Tracker
VariableStartAfter Step 2After Step 4After Step 6Final
main branch contentInitial codeCode + Change ACode + Change ACode + Change A + Change BCode + Change A + Change B
feature branch contentInitial codeInitial codeCode + Change BCode + Change BCode + Change B
Key Moments - 3 Insights
Why does merging not overwrite changes from the main branch?
Because merging combines changes from both branches, preserving commits from main and feature as shown in step 6 of the execution_table.
What happens if both branches change the same line?
A conflict occurs that must be resolved manually before the merge completes, as noted in step 7 of the execution_table.
Why do we checkout main before merging?
Because merging applies changes into the current branch, so we switch to main to merge feature into it (step 5 and 6).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what changes are present on the main branch after step 2?
ANo new changes
BChange A committed
CChange B committed
DChanges A and B combined
💡 Hint
Check the 'Changes Present' column at step 2 in the execution_table.
At which step does the merge combine changes from both branches?
AStep 4
BStep 3
CStep 6
DStep 7
💡 Hint
Look for the step where 'Changes A and B combined' appears in the execution_table.
If a conflict occurs during merge, which step would show this in the execution_table?
AStep 7
BStep 6
CStep 5
DStep 2
💡 Hint
Step 7 checks for conflicts according to the execution_table.
Concept Snapshot
git merge combines changes from two branches into one.
Switch to the branch you want to update (e.g., main).
Run 'git merge <branch>' to combine work.
If changes overlap, resolve conflicts manually.
Merge preserves history from both branches.
Full Transcript
Merging in git means combining work from two branches. First, you have two branches, each with their own changes. You commit changes on each branch separately. Then, you switch to the branch you want to update, usually main. Running 'git merge feature' takes the changes from the feature branch and adds them to main. If both branches changed different parts, git combines them automatically. If they changed the same lines, git asks you to fix conflicts. After resolving conflicts, the merge finishes and you have one branch with all combined work.

Practice

(1/5)
1. What does the git merge command do in a project?
easy
A. It creates a new branch from the current branch.
B. It deletes a branch permanently.
C. It combines changes from one branch into another branch.
D. It shows the history of commits in the current branch.

Solution

  1. Step 1: Understand the purpose of git merge

    The git merge command is used to combine changes from one branch into another branch.
  2. Step 2: Compare with other git commands

    Deleting branches is done with git branch -d, creating branches with git branch, and viewing history with git log. None of these combine work like merge.
  3. Final Answer:

    It combines changes from one branch into another branch. -> Option C
  4. Quick Check:

    Merge = combine changes [OK]
Hint: Merge means combining work from branches [OK]
Common Mistakes:
  • Confusing merge with branch deletion
  • Thinking merge creates new branches
  • Mixing merge with viewing commit history
2. Which of the following is the correct syntax to merge a branch named feature into your current branch?
easy
A. git merge feature
B. git merge -b feature
C. git merge --create feature
D. git merge -d feature

Solution

  1. Step 1: Recall the git merge syntax

    The correct syntax to merge a branch is git merge <branch-name>. So for branch 'feature', it is git merge feature.
  2. Step 2: Identify incorrect options

    Options with flags like -b, --create, or -d are not valid for merging branches.
  3. Final Answer:

    git merge feature -> Option A
  4. Quick Check:

    Merge syntax = git merge branch-name [OK]
Hint: Use 'git merge branch-name' to combine branches [OK]
Common Mistakes:
  • Adding wrong flags like -b or -d
  • Confusing merge with branch creation
  • Using incorrect command order
3. Given the following commands run in order:
git checkout main
git merge feature

What happens after these commands?
medium
A. The 'feature' branch is deleted.
B. The 'main' branch is reset to the state of 'feature'.
C. A new branch named 'feature' is created.
D. The changes from the 'feature' branch are combined into 'main'.

Solution

  1. Step 1: Analyze the commands

    First, git checkout main switches to the 'main' branch. Then, git merge feature merges changes from 'feature' into 'main'.
  2. Step 2: Understand the effect of merge

    The merge combines the work from 'feature' into 'main' without deleting or resetting branches.
  3. Final Answer:

    The changes from the 'feature' branch are combined into 'main'. -> Option D
  4. Quick Check:

    Checkout + merge = combine changes [OK]
Hint: Checkout target branch, then merge source branch [OK]
Common Mistakes:
  • Thinking merge deletes branches
  • Confusing merge with reset
  • Assuming merge creates new branches
4. You ran git merge feature but got a conflict error. What should you do next?
medium
A. Delete the 'feature' branch to fix the conflict.
B. Manually resolve conflicts in files, then commit the merge.
C. Run git merge --abort and ignore the changes.
D. Run git reset --hard to force merge.

Solution

  1. Step 1: Understand merge conflicts

    When a conflict occurs, Git stops the merge and asks you to fix conflicting files manually.
  2. Step 2: Resolve conflicts and complete merge

    You must open the conflicting files, fix the differences, then commit the merge to finish combining work.
  3. Final Answer:

    Manually resolve conflicts in files, then commit the merge. -> Option B
  4. Quick Check:

    Conflicts require manual fix + commit [OK]
Hint: Fix conflicts manually, then commit merge [OK]
Common Mistakes:
  • Deleting branches to fix conflicts
  • Aborting merge without resolving
  • Using reset to force merge ignoring conflicts
5. You have two branches: main and feature. Both have new commits. You want to combine them so main has all changes, but keep the history clear. Which sequence is best?
hard
A. Checkout main, run git merge feature, then push main.
B. Checkout feature, run git merge main, then delete main.
C. Delete feature, then copy files manually to main.
D. Checkout main, run git rebase feature, then push main.

Solution

  1. Step 1: Identify the goal

    You want main to have all changes from both branches and keep history clear.
  2. Step 2: Choose the correct merge approach

    Checking out main and merging feature combines work properly and keeps history intact. Rebasing rewrites history and is more complex.
  3. Final Answer:

    Checkout main, run git merge feature, then push main. -> Option A
  4. Quick Check:

    Merge feature into main to combine work [OK]
Hint: Merge feature into main branch to combine work safely [OK]
Common Mistakes:
  • Merging main into feature instead of the reverse
  • Deleting branches before merging
  • Using rebase without understanding history rewrite