Git rebase moves or replays a series of commits onto a new base commit. Instead of joining two branches with a merge commit, git rebase takes the commits that exist only on your branch, sets them aside, moves your branch to the tip of another branch, and then applies your commits again one by one. The result is a straight, linear history, but the replayed commits are new commits with new hashes.
This guide explains how git rebase works step by step, how it differs from merge, how to resolve conflicts during it, and the rule that keeps it safe.
Suppose main and a feature branch diverged from commit B:
Merging main into feature would add a merge commit. Rebasing instead re-applies C and D on top of F:
C' and D' have the same changes and messages as C and D, but they are different commits, because a commit's hash depends on its parent. The old C and D are not deleted at once; they become unreachable and are cleaned up later by garbage collection, and you can still find them with git reflog for a while.
Switch to the branch you want to move and rebase it onto the target:
Git works through your commits one at a time. When it finishes, git log --oneline --graph shows a single straight line. You can then fast-forward main to include the feature without any merge commit:
The --ff-only option makes the command fail instead of creating a merge commit if a fast-forward is not possible, which is a handy safety check.
During a rebase Git performs these steps:
Because every commit is applied separately, a conflict can show up at any step, not just once as with a merge.
When a patch does not apply cleanly, Git stops and tells you which file conflicts:
Fix the file, mark it resolved, and continue:
Two other commands are important:
| Command | Effect |
|---|---|
git rebase --abort | Stops and returns the branch to the state before the rebase began |
git rebase --skip | Drops the commit that is causing the conflict and moves to the next one |
If you feel lost, git rebase --abort is always safe and puts everything back.
| Aspect | Merge | Rebase |
|---|---|---|
| History | Keeps the true branching shape | Linear, as if work happened in sequence |
| Merge commit | Created | Not created |
| Existing commits | Unchanged | Replaced by new commits |
| Conflicts | Resolved once | May be resolved per commit |
| Safe on shared branches | Yes | No |
Neither is better in general. Merge records what really happened; rebase gives a tidier story. Many teams rebase their own feature branch onto main before opening a pull request and then merge it normally.
Never rebase commits that other people already have, such as main or a branch several teammates use. Rebasing rewrites those commits, and when you push, teammates' copies no longer match yours, which causes duplicated commits and painful conflicts.
Rebasing your own branch that nobody else uses is fine. If you have already pushed that branch, a rebase needs a forced push afterwards, and you should use the safer form:
This refuses to overwrite the remote branch if someone else added commits you have not seen, unlike a plain --force.
git pull --rebase fetches and rebases your local commits on top of the remote branch instead of creating a merge commit. You can make this the default with git config --global pull.rebase true.git rebase -i HEAD~3 opens an interactive rebase to reorder, squash or edit the last three commits.git rebase --onto moves a range of commits to a different base, which is useful when a branch was started from the wrong parent.No. It creates new copies and moves the branch pointer. The old commits stay reachable through git reflog for some time, so you can recover them.
It is different, not better. Use rebase to keep your own branch current and tidy, and use merge for shared branches or when you want the exact history.
During a rebase use git rebase --abort. After it finished, find the earlier state in git reflog and run git reset --hard <hash>, bearing in mind that this discards uncommitted work.
Your local branch history no longer matches the remote branch. Push with git push --force-with-lease if you are sure that nobody else uses the branch.
Git rebase replays your commits on a new base, producing a linear history made of new commits. Resolve conflicts commit by commit, use --abort if needed, and follow the golden rule: rebase only commits that exist only on your machine or on a branch that nobody else uses.