Git Notes
Learn how to merge branches in Git including three-way merges, merge strategies, and best practices for combining feature branches into main development lines.
Merging is the process of combining work from different branches into a single branch. It is how features, fixes, and changes get integrated into the main codebase.
How Merging Works
Git merging takes two branch tips and finds their common ancestor, then creates a new commit that combines changes from both branches:
Basic Merge Workflow
# Step 1: Switch to the target branch (where you want changes merged INTO)
git checkout main
# Step 2: Make sure target is up to date
git pull origin main
# Step 3: Merge the source branch
git merge feature-branchMerge made by the 'ort' strategy. src/feature.js | 45 +++++++++++++++++++++++++++++++++++++++++++++ src/utils.js | 12 ++++++++++++ tests/test.js | 28 ++++++++++++++++++++++++++++ 3 files changed, 85 insertions(+) create mode 100644 src/feature.js
Step-by-Step Tutorial
# Create a project with some history
git init merge-demo && cd merge-demo
echo "# Project" > README.md
git add . && git commit -m "Initial commit"
# Create feature branch
git checkout -b feature/add-navigation
# Work on the feature
echo "<nav>Home | About | Contact</nav>" > nav.html
git add . && git commit -m "Add navigation HTML"
echo "nav { display: flex; }" > nav.css
git add . && git commit -m "Add navigation styles"
# Switch back to main and add some changes there too
git checkout main
echo "Welcome to our site" > index.html
git add . && git commit -m "Add homepage content"
# Now merge the feature into main
git merge feature/add-navigationMerge made by the 'ort' strategy. nav.css | 1 + nav.html | 1 + 2 files changed, 2 insertions(+) create mode 100644 nav.css create mode 100644 nav.html
# View the merge in history
git log --oneline --graph* f8e7d6c (HEAD -> main) Merge branch 'feature/add-navigation' |\ | * d5e6f7g Add navigation styles | * c4d5e6f Add navigation HTML * | b3c4d5e Add homepage content |/ * a1b2c3d Initial commit
Merge Strategies
Recursive/ORT Strategy (Default)
The default strategy for merging two branches:
git merge feature-branch
# Uses 'ort' strategy (default in Git 2.33+)Octopus Strategy
For merging more than two branches simultaneously:
git merge feature-1 feature-2 feature-3Ours Strategy
Keeps the current branch's version for everything:
git merge -s ours abandoned-branchSubtree Strategy
For merging a project as a subdirectory:
git merge -s subtree library-branchMerge Commit Messages
# Default message
git merge feature-branch
# Message: "Merge branch 'feature-branch'"
# Custom message
git merge feature-branch -m "Merge: Add user authentication system"
# No commit (merge but don't auto-commit)
git merge --no-commit feature-branch
# Review changes, then manually commit
git commit -m "Merge feature with additional adjustments"Merge Options
# Squash merge - combines all commits into one (no merge commit)
git merge --squash feature-branch
git commit -m "Add feature (squashed)"
# No fast-forward - always create merge commit
git merge --no-ff feature-branch
# Abort a merge in progress
git merge --abort
# Continue after resolving conflicts
git merge --continueSquash Merge Explained
Regular merge
main [A]──[B]──────────[M]
\ /
feature [C]──[D]─┘
Squash merge
main [A]──[B]──[CD] (single commit with all feature changes)
\
feature [C]──[D] (branch unchanged)
git checkout main
git merge --squash feature/lots-of-commits
git commit -m "Add complete feature (squashed from 15 commits)"Squash commit -- not updating HEAD Automatic merge went well; stopped before committing as requested
Verifying Merges
# Check if a branch has been merged
git branch --merged mainfeature/completed hotfix/security-fix * main
# See merge commit details
git log --merges --onelinef8e7d6c Merge branch 'feature/add-navigation' a9b8c7d Merge branch 'hotfix/security-fix'
# See what a merge brought in
git log main..feature-branch --onelineBest Practices for Merging
- Always pull before merging — Ensure your target branch is up to date
- Test before merging — Run tests on both branches
- Use Pull Requests — Get code review before merging
- Write meaningful merge messages — Describe what the merge brings
- Delete merged branches — Keep the repository clean
- Prefer small, frequent merges — Reduces conflict complexity
# Complete merge workflow
git checkout main
git pull origin main
git merge --no-ff feature/user-auth -m "Merge: User authentication system"
git push origin main
git branch -d feature/user-auth
git push origin --delete feature/user-authInterview Questions
- What is a three-way merge in Git?
A three-way merge uses three commits — the two branch tips and their common ancestor — to determine what changed on each branch and combine them into a new merge commit.
- What is the difference between
git mergeandgit merge --squash?
Regular merge creates a merge commit preserving branch history. Squash merge combines all branch commits into a single commit on the target branch without a merge commit, creating a cleaner but less detailed history.
- How do you undo a merge that has already been committed?
Use git revert -m 1 <merge-commit-hash> to create a new commit that reverses the merge. The -m 1 specifies which parent to keep (usually the mainline).
- What does
git merge --no-commitdo?
It performs the merge but stops before creating the merge commit, allowing you to review the merged result, make additional changes, or customize the commit message.
- Can you merge multiple branches at once?
Yes, Git supports octopus merges: git merge branch1 branch2 branch3. This works as long as there are no conflicts between the branches.
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Merge Branches in Git - Combine Development Work.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this Git & GitHub topic.
Search Terms
git-github, git & github, git, github, branching, and, merging, merge
Related Git & GitHub Topics