Git Rebase Visualizer
Git rebase moves your branch so it starts from the tip of another branch, replaying each of your commits as a new commit with a new ID. Git merge keeps every commit as it is and joins the two histories with one merge commit.
Predict first
Guess what each command will do, then check your answers in the visualizer below.
Question 1 of 3
Your feature branch has commits X and Y. You run git rebase main. What happens to X and Y?
Rebase vs merge, side by side
Both panels start from the same commit graph. Step through each one and watch the commit IDs. The IDs here are short sample hashes, but like real Git IDs they change whenever a commit’s parent changes.
git rebase
git switch feature
git rebase mainStep 1 of 6: Before
Your feature branch has X, Y. Since you branched off at B, main has moved on.
- X c4ba170 Add search box
- Y 8f53930 Style search results
git merge
git switch feature
git merge mainStep 1 of 3: Before
The same starting point: X, Y on feature, and new commits on main.
- X c4ba170 Add search box
- Y 8f53930 Style search results
Key: gold ring = merge base, green = newly created commit, dashed = set aside or no longer on a branch.
Rebase vs merge at a glance
| Aspect | git rebase | git merge |
|---|---|---|
| History shape | One straight line | Two lines joined by a merge commit |
| Existing commit IDs | Your branch’s commits get new IDs | Every commit keeps its ID |
| New commits | A copy of each of your commits | One merge commit |
| Conflicts | Resolved commit by commit as each one replays | Resolved once, in the merge commit |
| Safe on shared branches | No, it rewrites history others may have | Yes |
How to rebase a feature branch onto main
- Get the latest main. Run git fetch origin so your copy of origin/main is current.
- Switch to your branch. Run git switch feature, replacing feature with your branch name.
- Start the rebase. Run git rebase origin/main. Git replays your commits one at a time on top of it.
- Resolve any conflicts. If Git stops on a conflict, fix the files, run git add on them, then git rebase --continue. Run git rebase --abort to go back to where you started.
- Update the remote branch. If you had already pushed the branch, run git push --force-with-lease. It overwrites the old commits only if no one else pushed in the meantime.
Merge or rebase?
Pick the safer choice for each situation. The deciding question is usually whether anyone else already has the commits.
Question 1 of 4
Your feature branch is only on your laptop. You want to catch up with main before opening a pull request.
The golden rule of rebasing
Don’t rebase commits that other people have already pulled. Their copies still point at the old IDs, so the next time they pull, Git sees two versions of the same work. If a rebase goes wrong on your own branch, nothing is lost straight away: run git reflog, find the commit from before the rebase, and run git reset --hard with that ID.
Frequently asked questions
What does git rebase do?
Git rebase takes the commits on your branch and replays them on top of another branch, such as main. The changes stay the same, but each replayed commit is a new commit with a new ID, and your branch ends up starting from the tip of main.
Is git rebase better than git merge?
Neither is better everywhere. Rebase gives a clean, linear history and suits branches only you use. Merge keeps history exactly as it happened and is the safe choice for branches other people share.
Why do I need to force push after a rebase?
The remote still has your old commits, and the rebased ones have new IDs, so a normal push is rejected. Use git push --force-with-lease, which refuses to overwrite the branch if someone else pushed to it since you last fetched.
How do I undo a git rebase?
Run git reflog to find the commit your branch pointed to before the rebase, then run git reset --hard with that ID. Right after a rebase, ORIG_HEAD usually points there too. Hard reset discards uncommitted changes, so commit or stash them first.
What is interactive rebase?
git rebase -i opens a list of your commits so you can reorder, reword, squash or drop them before they are replayed. It is commonly used to tidy a branch before opening a pull request.
Rebasing branches your AI coding assistant wrote? Code Card turns your AI coding activity into a shareable developer profile.