Merging
Branching & Merging
Preview
Merge
git switch main\ngit merge feature-navFast-forward vs merge commit
- Fast-forward: branch is ahead, history is straight
- Merge commit: histories diverged, Git joins them
The happy case
main: A---B\nfeature: \---C---D\nafter merge: A---B---C---DA divergent history
main: A---B---E\nfeature: \---C---D\nmerge result: A---B---E---M\n \---C---D---/Rebase (alternative)
git switch feature\ngit rebase main\n# replays your commits on top of main — cleaner historyRebase rewrites history, so never rebase shared branches. On your own branch it keeps history linear.
Practice pattern
- Create a branch from main
- Commit work in small steps
- Return to main, merge the branch
- Delete the merged branch
Tip: Merge brings work in; rebase keeps history tidy. Default to merge until you are comfortable with both.
This is a free preview lesson
Enroll in the Git & GitHub for Beginners to unlock every lesson, lesson PDF guides, quizzes and your certificate - and start creating your own pieces today.