Privacy Policy
© 2025 linux101.dev

Git: Merge and Rebase

Merge and rebase are two ways to integrate changes from one branch into another.

Merging

Merge a branch

git switch main

Switches to the main branch.

git merge feature-branch

Integrates commits from the 'feature-branch' into the current branch using a merge commit. This preserves the history of both branches.

Rebasing

Rebase a branch

git switch feature-branch

Switches to the 'feature-branch'.

git rebase main

Moves the entire 'feature-branch' to the tip of 'main', creating a linear history. This rewrites the commit history of the 'feature-branch'.

Difference Between Merge and Rebase

Merge:
  • Combines the histories of two branches.
  • Creates a merge commit if there are changes on both branches.
  • Preserves the complete history of both branches.
Rebase:
  • Reapplies commits from one branch onto another.
  • Rewrites commit history to create a linear sequence.
  • Useful for cleaning up commit history before merging into a main branch.
  • Can be more complex if there are conflicts, as it requires resolving them for each commit.