Rebase re-applies your branch's commits on top of another branch, creating a linear history. Unlike merge, it rewrites commit hashes. Use rebase to integrate upstream changes into a feature branch before opening a PR. Never rebase commits that have been pushed to a shared branch.
Git
Beginner
11 min read
git rebase: Moving Commits
Example
# Situation:
# main: A - B - C - D
# feature: E - F
# After: git switch feature && git rebase main
# main: A - B - C - D
# feature: E' - F' (new commits with new hashes)
git switch feature/user-login
git rebase main
# If conflicts occur during rebase:
# 1. Fix the conflict in the file
# 2. Stage the fix:
git add src/auth.js
# 3. Continue the rebase:
git rebase --continue
# or abort and go back to the original state:
git rebase --abort
# After rebasing, force-push to update your PR branch
# (safe when only you use the branch):
git push --force-with-lease origin feature/user-login
# --force-with-lease is safer than --force:
# it fails if the remote has changes you haven't seen.
# Rebase vs Merge:
# Merge — preserves full history, adds merge commits
# Rebase — creates clean linear history, rewrites hashes