Course Git & GitHub for Beginners

Merging

Branching & Merging Preview

Merge

git switch main\ngit merge feature-nav

Fast-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---D

A 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 history

Rebase rewrites history, so never rebase shared branches. On your own branch it keeps history linear.

Practice pattern

  1. Create a branch from main
  2. Commit work in small steps
  3. Return to main, merge the branch
  4. 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.


Discussion

Free preview lesson