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
Merge Commit Creation
📖 Scenario: You are working on a project with two branches: main and feature. You want to combine the changes from the feature branch into main using a merge commit. This is a common task when multiple people work on different parts of a project.
🎯 Goal: Learn how to create a merge commit by merging the feature branch into the main branch using Git commands.
📋 What You'll Learn
Create a new Git repository
Create an initial commit on the main branch
Create and switch to a branch called feature
Make a commit on the feature branch
Switch back to main branch
Merge the feature branch into main with a merge commit
Show the commit history to confirm the merge commit
💡 Why This Matters
🌍 Real World
Merging branches with a merge commit is common when combining work from different team members or features in software projects.
💼 Career
Understanding merge commits is essential for collaboration in software development and version control workflows.
Progress0 / 4 steps
1
Initialize repository, create initial commit, and create feature branch
Initialize a new Git repository with git init. Create a file initial.txt with content This is the initial file. using echo "This is the initial file." > initial.txt. Add it with git add initial.txt and commit it with git commit -m "Initial commit". Then create and switch to a branch called feature using git checkout -b feature.
Git
Hint
Use git init to start a new repo. Use echo "This is the initial file." > initial.txt, then git add initial.txt and git commit -m "Initial commit". Finally, use git checkout -b feature to create and switch to the feature branch.
2
Create a commit on the feature branch
Create a new file called feature.txt with content This is a feature file. using echo "This is a feature file." > feature.txt. Add it to Git with git add feature.txt and commit it with the message "Add feature file" using git commit -m "Add feature file".
Git
Hint
Use echo "This is a feature file." > feature.txt to create the file. Then add and commit it with the exact commit message.
3
Switch back to main branch
Switch back to the main branch using git checkout main.
Git
Hint
Use git checkout main to switch branches.
4
Merge feature branch into main and show commit history
Merge the feature branch into main using git merge feature. Then show the commit history with git log --oneline --graph --all to confirm the merge commit.
Git
Hint
Use git merge feature to merge. Then use git log --oneline --graph --all to see the merge commit in the history.
Practice
(1/5)
1. What does a git merge <branch-name> command do in Git?
easy
A. Combines changes from the specified branch into the current branch with a merge commit
B. Deletes the specified branch from the repository
C. Creates a new branch named after the specified branch
D. Resets the current branch to the specified branch's state without merging
Solution
Step 1: Understand the purpose of git merge
The git merge <branch-name> command is used to combine changes from another branch into the current branch.
Step 2: Identify the result of the merge
This operation creates a merge commit that records the integration of changes from both branches.
Final Answer:
Combines changes from the specified branch into the current branch with a merge commit -> Option A
Quick Check:
Merge command = combine branches with commit [OK]
Hint: Merge means combine branches with a commit [OK]
Common Mistakes:
Confusing merge with branch deletion
Thinking merge creates a new branch
Assuming merge resets branch without commit
2. Which of the following is the correct syntax to create a merge commit from branch feature into the current branch?
easy
A. git merge feature
B. git merge -b feature
C. git merge --delete feature
D. git merge --reset feature
Solution
Step 1: Recall the basic merge syntax
The correct syntax to merge a branch is git merge <branch-name> without extra flags for a normal merge commit.
Step 2: Evaluate the options
git merge feature matches the correct syntax. The other options use invalid or unrelated flags.
Final Answer:
git merge feature -> Option A
Quick Check:
Simple merge = git merge branch [OK]
Hint: Use 'git merge branch-name' to merge simply [OK]
Common Mistakes:
Adding unnecessary flags like -b or --delete
Confusing merge with branch creation or deletion commands
Using reset flag which is unrelated to merge
3. Given the following commands executed in order:
git checkout main
git merge feature
What will Git do if there are no conflicts between main and feature and the branches have diverged?
medium
A. Reset main branch to feature branch state without commit
B. Delete the feature branch automatically
C. Create a merge commit combining changes from feature into main
D. Fail with an error asking to resolve conflicts
Solution
Step 1: Understand the merge process without conflicts
If there are no conflicts and the branches have diverged, Git will automatically create a merge commit combining changes from the feature branch into main.
Step 2: Confirm no branch deletion or errors occur
Git does not delete branches or reset branches automatically during merge without conflicts.
Final Answer:
Create a merge commit combining changes from feature into main -> Option C
Quick Check:
No conflicts + diverged = auto merge commit [OK]
Hint: No conflicts + diverged means merge commit created automatically [OK]
Common Mistakes:
Thinking feature branch is deleted after merge
Expecting errors when no conflicts exist
Confusing merge with reset or branch deletion
4. You run git merge feature but Git reports conflicts. What should you do next to complete the merge?
medium
A. Delete the current branch and recreate it to fix conflicts
B. Run git merge --abort to cancel the merge and delete the feature branch
C. Run git reset --hard to force merge without fixing conflicts
D. Manually fix conflicts in files, then run git add and git commit
Solution
Step 1: Understand conflict resolution process
When Git reports conflicts, you must manually fix the conflicting files by editing them.
Step 2: Complete the merge after fixing conflicts
After fixing, stage the changes with git add and finish the merge with git commit.
Final Answer:
Manually fix conflicts in files, then run git add and git commit -> Option D
Quick Check:
Fix conflicts, add, commit to complete merge [OK]
Hint: Fix conflicts manually, then add and commit [OK]
Common Mistakes:
Aborting merge and deleting branches unnecessarily
Using reset to skip conflict resolution
Deleting branches instead of resolving conflicts
5. You want to merge branch feature into main but avoid creating a merge commit. Which command should you use?
hard
A. git merge --no-ff feature
B. git merge --squash feature
C. git merge --ff-only feature
D. git merge --abort feature
Solution
Step 1: Understand merge commit creation options
The --squash option merges changes without creating a merge commit by combining all changes into one commit.
Step 2: Compare other options
--no-ff forces a merge commit, --ff-only only merges if fast-forward is possible, and --abort cancels merges.
Final Answer:
git merge --squash feature -> Option B
Quick Check:
Squash merges without merge commit [OK]
Hint: Use --squash to merge without merge commit [OK]