Merging Branches
9 min read
Combining Work with git merge
Merging integrates changes from one branch into another. You always merge *into* the branch you are currently on.
bash
git switch main
git merge feature/loginFast-Forward vs Merge Commit
Fast-forward merge: if main has not moved since you branched, Git simply moves the main pointer forward to your branch tip. No new commit is created — the history is linear.
Merge commit: if main has new commits since your branch diverged, Git creates a new commit with two parents. This preserves the fact that two parallel lines of work existed.
bash
git merge --no-ff feature/login # force a merge commit even when FF is possible
git merge --squash feature/login # squash all branch commits into one staged changeAt CPCODELAB, PR merges use squash and merge on GitHub so each feature appears as one clean commit on main, regardless of how many "wip" commits were on the branch.
Ready to test yourself?
Practice Git & GitHub— quiz & coding exercises