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
Using the git merge Command
📖 Scenario: You are working on a project with two branches: main and feature. You want to combine the changes from the feature branch into the main branch to update the main project with new features.
🎯 Goal: Learn how to use the git merge command to combine changes from one branch into another safely and correctly.
📋 What You'll Learn
Create a new branch called feature from main
Make a commit on the feature branch
Switch back to the main branch
Merge the feature branch into main using git merge
💡 Why This Matters
🌍 Real World
Merging branches is a daily task in software development to combine new features or fixes into the main project code.
💼 Career
Understanding how to merge branches correctly is essential for collaboration in teams and managing code changes safely.
Progress0 / 4 steps
1
Create the feature branch and make a commit
Create a new branch called feature from main using git branch feature. Then switch to the feature branch using git checkout feature. Finally, create a new file called feature.txt with the content New feature added and commit it with the message Added feature.txt.
Git
Hint
Use git branch to create the branch, git checkout to switch, then create and commit the file.
2
Switch back to the main branch
Switch back to the main branch using git checkout main.
Git
Hint
Use git checkout main to switch back to the main branch.
3
Merge the feature branch into main
Use the git merge feature command to merge the changes from the feature branch into the main branch.
Git
Hint
Run git merge feature while on the main branch to combine changes.
4
Verify the merge result
Run git log --oneline and cat feature.txt to verify that the feature.txt file from the feature branch is now in the main branch.
Git
Hint
Use git log --oneline to see commits and cat feature.txt to see the file content.
Practice
(1/5)
1. What does the git merge command do in Git?
easy
A. It combines changes from one branch into the current branch.
B. It deletes a branch permanently.
C. It creates a new branch from the current branch.
D. It shows the commit history of the current branch.
Solution
Step 1: Understand the purpose of git merge
The git merge command is used to combine changes from one branch into another branch.
Step 2: Identify the correct description
Among the options, only It combines changes from one branch into the current branch. correctly describes merging changes into the current branch.
Final Answer:
It combines changes from one branch into the current branch. -> Option A
Quick Check:
git merge = combine branches [OK]
Hint: Merge means combine changes into current branch [OK]
Common Mistakes:
Confusing merge with branch deletion
Thinking merge creates a new branch
Mixing merge with viewing history
2. Which of the following is the correct syntax to merge branch feature into the current branch?
easy
A. git merge --create feature
B. git merge -b feature
C. git merge feature
D. git merge --delete feature
Solution
Step 1: Recall the basic merge syntax
The correct syntax to merge a branch is git merge branch_name.
Step 2: Match the syntax with options
git merge feature matches the correct syntax. Options B, C, and D use invalid flags or commands.
Final Answer:
git merge feature -> Option C
Quick Check:
git merge + branch name = correct syntax [OK]
Hint: Use 'git merge branch_name' to merge branches [OK]
Common Mistakes:
Adding incorrect flags like -b or --create
Confusing merge with branch creation or deletion
Using merge without specifying branch name
3. Given the following commands run in order:
git checkout main
git merge feature
What happens if the feature branch has commits not in main?
medium
A. The main branch is reset to feature branch state.
B. The feature branch is deleted automatically.
C. Git throws an error and stops the merge.
D. The commits from feature are added to main.
Solution
Step 1: Understand merge behavior with new commits
When merging a branch with new commits, Git combines those commits into the current branch.
Step 2: Analyze the options
The commits from feature are added to main correctly describes this behavior. Options B, C, and D describe incorrect or unrelated behaviors.
Final Answer:
The commits from feature are added to main. -> Option D
Quick Check:
Merge adds commits [OK]
Hint: Merge adds commits, does not delete branches [OK]
Common Mistakes:
Thinking merge deletes branches
Expecting merge to reset branches
Assuming merge always fails with new commits
4. You run git merge feature but Git reports a conflict. What should you do next?
medium
A. Restart Git to clear the conflict.
B. Manually resolve the conflicts in files, then run git add and git commit.
C. Run git merge --force to ignore conflicts.
D. Delete the feature branch and try merging again.
Solution
Step 1: Understand merge conflicts
When Git reports conflicts, it means changes clash and need manual fixing.
Step 2: Resolve conflicts properly
You must edit conflicted files to fix issues, then stage and commit the changes to complete the merge.
Final Answer:
Manually resolve the conflicts in files, then run git add and git commit. -> Option B
Quick Check:
Fix conflicts manually, then add and commit [OK]
Hint: Fix conflicts manually, then add and commit [OK]
Common Mistakes:
Trying to force merge ignoring conflicts
Deleting branches to fix conflicts
Restarting Git expecting conflicts to clear
5. You want to merge branch feature into main but keep main's changes in case of conflict (favor main). Which command helps achieve this?
hard
A. git merge -X ours feature
B. git merge --no-ff feature
C. git merge -s recursive feature
D. git merge --abort feature
Solution
Step 1: Understand merge strategies
The -X ours option tells Git to favor the current branch's changes during conflicts.
Step 2: Match the option to the goal
git merge -X ours feature uses -X ours to keep main's changes if conflicts occur. Other options do not achieve this.
Final Answer:
git merge -X ours feature -> Option A
Quick Check:
Use -X ours to favor current branch on conflicts [OK]
Hint: Use 'git merge -X ours' to keep current branch changes [OK]