Git Notes
Merging is the process of combining the work from two different branches into one. It is how features developed in isolation come together to form a...
Merging is the process of combining the work from two different branches into one. It is how features developed in isolation come together to form a complete application. Understanding how Git merges work — and what happens when they go wrong — is essential knowledge for any developer working in a team.
What Happens During a Merge
When you merge one branch into another, Git looks at three specific points: the tip of your current branch, the tip of the branch you are merging in, and their common ancestor (the commit where the two branches diverged). Git uses these three snapshots to figure out what changed on each side and combines those changes.
If the same part of the same file was modified differently on each branch, Git cannot automatically decide which version to keep — this results in a merge conflict that requires human intervention.
Types of Merges
Fast-Forward Merge
A fast-forward merge occurs when there is a direct linear path between your current branch and the branch you are merging. In this case, Git simply moves the branch pointer forward — no new commit is created.
git checkout main
git merge feature-loginIf main has not received any new commits since feature-login branched off, Git performs a fast-forward:
Before
main: A---B
\
feature-login: C---D
After (fast-forward)
main: A---B---C---D
Three-Way Merge
When both branches have diverged (both have new commits since their common ancestor), Git performs a three-way merge and creates a new merge commit:
git checkout main
git merge feature-loginBefore
main: A---B---E---F
\
feature-login: C---D
After (three-way merge)
main: A---B---E---F---G (merge commit)
\ /
feature-login: C---D----
The merge commit G has two parents — it represents the point where both lines of development came together.
Performing a Basic Merge
The standard workflow for merging a feature branch into main:
# Make sure you are on the receiving branch
git checkout main
# Pull latest changes from remote
git pull origin main
# Merge the feature branch
git merge feature/user-profile
# Push the result
git push origin mainMerge Options and Flags
No fast-forward merge: Forces creation of a merge commit even when fast-forward is possible. This preserves the history that a feature was developed on a separate branch:
git merge --no-ff feature/user-profileSquash merge: Combines all commits from the feature branch into a single commit on the target branch. The feature branch history is not preserved:
git merge --squash feature/user-profile
git commit -m "Add user profile feature"Abort a merge: If you are in the middle of a conflicted merge and want to start over:
git merge --abortHandling Merge Conflicts
Conflicts occur when Git cannot automatically reconcile differences. When a conflict arises, Git marks the affected files and pauses the merge:
$ git merge feature/redesign
Auto-merging src/App.js
CONFLICT (content): Merge conflict in src/App.js
Automatic merge failed; fix conflicts and then commit the result.Git marks conflicts in the file with special markers:
<<<<<<< HEAD
const theme = 'dark';
=======
const theme = 'light';
>>>>>>> feature/redesignThe section between <<<<<<< HEAD and ======= is your current branch's version. The section between ======= and >>>>>>> is from the incoming branch.
To resolve the conflict:
- Open the conflicted file
- Decide which version to keep (or combine both)
- Remove the conflict markers
- Stage the resolved file and commit
# After manually fixing the file
git add src/App.js
git commit -m "Merge feature/redesign, resolve theme conflict"Merge Strategies
Git supports different merge strategies for complex scenarios:
- recursive (default): Handles most cases including criss-cross merges
- ours: Keeps everything from the current branch, ignoring incoming changes
- octopus: Used for merging more than two branches simultaneously
git merge -s ours legacy-branch # Keep our version entirely
git merge -X theirs feature-branch # Prefer their changes in conflictsBest Practices for Merging
Keep branches short-lived. The longer a feature branch lives, the more it diverges from main, and the more likely you are to face complex conflicts.
Pull main into your feature branch regularly:
git checkout feature/dashboard
git merge main
# Resolve any conflicts here, in your feature branchThis keeps your branch up-to-date and ensures the final merge into main goes smoothly.
Test after merging. Always run your test suite after a merge, even if there were no conflicts. Semantic conflicts (code that does not conflict textually but breaks logically) will not be caught by Git.
Real-World Merge Workflow
In a typical team environment, the merge workflow looks like this:
# Developer finishes feature
git checkout feature/notifications
git pull origin main # Get latest main
git merge main # Merge main into feature branch
# Fix any conflicts, run tests
git push origin feature/notifications
# After code review approval, merge via pull request
# Or manually:
git checkout main
git pull origin main
git merge --no-ff feature/notifications
git push origin main
git branch -d feature/notificationsKey Takeaways
Merging is how collaborative work comes together in Git. Fast-forward merges provide clean linear history, while three-way merges preserve the branching structure. Conflicts are normal and not something to fear — they simply indicate two people modified the same code and Git needs your judgment to reconcile the differences. Regular merging of main into your feature branches minimizes surprise conflicts at the end.
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Git Merge.
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