Rebasing is one of Git’s most powerful features for rewriting history. It allows you to transplant a series of commits onto a different starting point, creating a cleaner, more linear history.Documentation Index
Fetch the complete documentation index at: https://mintlify.com/git/git/llms.txt
Use this file to discover all available pages before exploring further.
What is Rebasing?
Rebasing takes a series of commits and “replays” them on top of a different base commit. Unlike merging, which creates a new commit to join two branches, rebasing rewrites the commit history to make it appear as if the work was done sequentially.Basic Rebasing
git add <filename>git rebase --continueInteractive Rebase
Interactive rebasing gives you complete control over your commit history, allowing you to reorder, edit, squash, or drop commits.Available Commands
pick- Use the commit as-isreword- Use the commit, but edit the commit messageedit- Use the commit, but stop for amendingsquash- Combine this commit with the previous one, keeping both messagesfixup- Like squash, but discard this commit’s messagedrop- Remove the commit entirelyexec- Run a shell command after this line
Example: Cleaning Up Commits
Advanced: Rebase with —onto
The--onto option allows you to transplant a branch from one base to another.
Transplanting a Topic Branch
Supposetopic is based on next, but you want to move it to master:
topic onto master:
Removing Commits from History
To remove commits F and G from a branch:Handling Merge Commits
By default, rebase drops merge commits. To preserve the branch structure, use--rebase-merges:
Autosquash Workflow
Use special commit messages to automatically mark commits for squashing:Recovering from Upstream Rebase
Rebase vs. Merge
| Aspect | Rebase | Merge |
|---|---|---|
| History | Linear, cleaner | Shows actual development history |
| Conflicts | Resolved per commit | Resolved once |
| Safety | Rewrites history | Preserves history |
| Use case | Local cleanup, feature branches | Integrating shared work |
Best Practices
- Never rebase public commits - Once commits are pushed and shared, rebasing them forces everyone downstream to fix their history
- Rebase before pushing - Clean up your local commits before sharing them
- Use interactive rebase for cleanup - Squash “fix typo” and “oops” commits before pushing
-
Test after rebasing - Each rebased commit should still compile and pass tests. Use
git rebase -i --exec "make test" -
Keep ORIG_HEAD in mind - Git saves your pre-rebase position in
ORIG_HEAD, so you can recover withgit reset --hard ORIG_HEAD
Common Scenarios
Updating a Feature Branch
Splitting a Commit
- Start interactive rebase:
git rebase -i <commit>^ - Mark the commit for editing: change
picktoedit - Reset to before the commit:
git reset HEAD^ - Stage and commit changes separately
- Continue:
git rebase --continue
Cleaning Up Before Merging
Configuration Options
Useful rebase configurations in your.gitconfig:
