Rebase keeps feature branches current and history clean, but misused it rewrites others' work.
Suppose you branched feature from main, made three commits, and main has moved on. Merging would add a merge commit. `git rebase main` (run on feature) instead takes your three commits, sets them aside, moves feature to the tip of main, and replays each commit on top, one by one.
The result looks as if you had started your work from today's main: a straight line, no merge commit, and a later merge into main becomes a fast-forward.
The catch: replayed commits are new commits with new IDs, because their parent changed. The old ones are abandoned. Hence the golden rule: don't rebase commits that others have based work on. Rebasing your own unpublished, or personal pushed, branch is fine; you'll then need git push --force-with-lease to update the remote copy.
Conflicts are resolved per replayed commit:
- Fix the files,
git addthem, thengit rebase --continue. git rebase --skipdrops the current commit;git rebase --abortreturns everything to how it was before you started.
Merge or rebase? Both are valid; teams pick a convention. Merge preserves exactly what happened; rebase produces a history that is easier to read and bisect. A common combination is rebasing your feature branch onto main to stay current, and merging it into main via a pull request.
git pull --rebase is the same idea for your local commits versus newly fetched upstream ones.
Example
git switch feature/search
git fetch origin
git rebase origin/main
# Successfully rebased and updated refs/heads/feature/search.
# On conflict:
# edit files, then
git add search.py
git rebase --continue # or --skip / --abort
git log --oneline --graph -5 # linear on top of origin/main
git push --force-with-lease # branch was pushed before: update itRemember
- Rebase replays your commits on a new base, producing new commit IDs
- Golden rule: don't rebase commits others have built on
- Resolve per commit: git add, then git rebase --continue
- After rebasing a pushed branch, use --force-with-lease
Watch out for
- Rebasing main (or any shared branch) onto something else and force-pushing — everyone else's history breaks.