What is the difference between Git merge and Git rebase

· Category: Git

Short answer

git merge creates a merge commit and preserves branch history, while git rebase replays commits on top of another branch for a linear history. Use merge for shared branches and rebase for local cleanup. When merge conflicts arise, see how to resolve Git merge conflicts. To move individual commits between branches, see how to cherry-pick a commit in Git.

Steps

  1. Merge approach: git checkout main, then git merge feature-branch
  2. Rebase approach: git checkout feature-branch, then git rebase main
  3. Resolve any conflicts during the process
  4. Continue rebase if needed: git rebase --continue
  5. Push changes; use git push --force-with-lease after rebasing if already pushed

Tips