Git Rebase

Git rebase moves your branch's commits so they start from a new base commit, usually the latest main. Instead of creating a merge commit, Git replays each of your commits on top, producing a clean, straight-line history.

Rebasing a Feature Branch onto main

Before: main moved on while you worked, so the branches diverged. After: your commits sit on top of the newest main, as if you had started your work today. Your commits get new hashes because they are new commits with a different parent.

git switch feature/search
git rebase main
Successfully rebased and updated refs/heads/feature/search.

Before and After

# before
* 7e8f9a0 (main) Update footer
| * 2c3d4e5 (feature/search) Add search results
| * 1b2c3d4 Add search box
|/
* a1b2c3d Add README

# after git rebase main
* 9f0a1b2 (feature/search) Add search results
* 8e9f0a1 Add search box
* 7e8f9a0 (main) Update footer
* a1b2c3d Add README
(main can now fast-forward to feature/search)

Handling Rebase Conflicts

Because commits are replayed one at a time, a conflict can stop the rebase at a specific commit. Fix the files, stage them, and continue.

git status                 # see conflicted files
# edit and fix the files
git add search.js
git rebase --continue

git rebase --skip          # drop this commit
git rebase --abort         # cancel and go back
Successfully rebased and updated refs/heads/feature/search.

Interactive Rebase

git rebase -i lets you edit your recent commits before sharing them: squash small "fix typo" commits, reword messages, reorder or drop commits.

git rebase -i HEAD~3

# editor opens:
pick 1b2c3d4 Add search box
squash 2c3d4e5 Fix typo in search box
reword 3d4e5f6 add results

# save and close; Git applies the plan
[detached HEAD 5e6f7a8] Add search box
Successfully rebased and updated refs/heads/feature/search.

Rebase vs Merge

  • Merge keeps the true history and adds merge commits; it never rewrites existing commits.
  • Rebase gives a linear, easier-to-read history but rewrites your commits.
  • Many teams rebase their own feature branches to stay up to date, then merge into main.
  • git pull --rebase updates your branch by rebasing local commits on top of the remote ones.

The Golden Rule of Rebasing

Never rebase commits that other people have already pulled, such as main or a shared branch. Rewriting shared history forces everyone else to fix their copies. If you rebase your own pushed feature branch, push with git push --force-with-lease, which refuses to overwrite commits you have not seen.

Frequently asked questions

What is the difference between git merge and git rebase?

Merge joins branches with a merge commit and keeps history as it happened. Rebase replays your commits onto another base, creating a linear history but rewriting those commits.

How do I squash commits in Git?

Run git rebase -i HEAD~N, change pick to squash (or s) for the commits you want to fold into the one above, save, then edit the combined message.

Is git rebase dangerous?

It is safe on your own local or personal branches. It becomes risky when you rebase commits others have based work on, because it rewrites shared history.