Git merge Command
In this page:
Fast-Forward Merges
A fast-forward merge happens when the target branch hasn't moved since your feature branch split off from it -- Git doesn't need a merge commit at all, it just slides the branch pointer forward to include the new commits.
Example: Fast-Forward Merges
git checkout main
git merge feature-x
Three-Way Merges
When both branches have gained their own separate commits since diverging, Git can't just fast-forward -- it performs a three-way merge, combining both histories and creating a new merge commit with two parents.
Example: Three-Way Merges
git checkout main
git merge feature-y
Squash Merges
A squash merge (git merge --squash) takes every commit from the source branch and flattens them into a single new commit on the target branch, trading detailed history for a cleaner, simpler main-branch log.
Example: Squash Merges
git checkout main
git merge --squash feature-z
git commit -m "Add feature z"
Handling Merge Conflicts
When the same lines of the same file were changed differently on both branches, Git can't automatically decide which version to keep -- it pauses the merge and marks the conflicting sections in the file for you to resolve by hand.
Example: Handling Merge Conflicts
git merge feature-x
# CONFLICT (content): Merge conflict in file.txt
git add file.txt
git commit
Verifying Merged Branches
git branch --merged lists branches whose commits are already fully included in your current branch, and --no-merged shows the opposite -- both are useful for finding stale feature branches that are safe to delete.
Example: Verifying Merged Branches
git branch --merged
git branch --no-merged
Chapter Quiz — Complete all 9 topics to unlock
0/9 topics done
Complete these topics first: