What is Git Rebase?
Git rebase moves or combines a series of commits to a new base commit.
It helps keep your project history clean and linear by replaying your changes on top of another branch, avoiding unnecessary merge commits.
When to Use Git Rebase
Use git rebase to:
- Keep a clean, linear history
- Avoid messy merge commits
- Combine multiple commits into one (squash)
- Edit or reorder commits before sharing
Basic Rebase
To apply your branch’s commits on top of another branch (e.g., bring your feature branch up to date with main):
git checkout feature-branch
git rebase main
This replays your feature branch commits on top of the latest main.
Interactive Rebase
Use interactive rebase to edit, reorder, squash, or reword commits:
git rebase -i <base>
For example, to rewrite the last 3 commits:
git rebase -i HEAD~3
An editor will open where you can:
- pick: keep the commit as-is
- reword: change the commit message
- edit: pause to change the commit itself
- squash / fixup: combine commits
Steps:
- Choose an action (pick, squash, etc.)
- Save and close the editor
- Git will apply changes step by step, letting you review or fix as needed
Continue, Abort, or Skip
During a rebase you might need to:
- Continue after resolving conflicts:
git add fixed_file.txt
git rebase --continue
- Abort to cancel and return to the original branch state:
git rebase --abort
- Skip a commit you can’t fix:
Review Changes
After finishing the rebase, always review your changes:
git log --oneline
git diff
Tips & Best Practices
- Rebasing rewrites commit history:
Avoid rebasing commits that have already been pushed to shared branches.
- Use git rebase -i before sharing to clean up commits.
- Resolve conflicts carefully and use git rebase --continue.
- Use git rebase --abort if things go wrong.
- Use git rebase --skip only when a commit can’t be fixed.
Troubleshooting
If conflicts occur: fix them and run:
git rebase --continue
If you can’t fix a commit:
git rebase --skip