Loading lessons...
Git Rebase
Git Rebase
git rebase reapplies your commits on top of a different base. It moves your branch's commits to a new starting point.
The rebase command
While on your feature branch:
git rebase main
Git takes your feature commits and replays them on top of the latest main. Your history ends up looking straight, as if you always started from the newest main.
Why rebase?
- Keep a clean, linear history with no extra merge commits.
- Pull in the latest changes from main before finishing a feature.
Rebase vs merge
- Merge preserves both histories and adds a merge commit.
- Rebase flattens history by moving your commits.
Both combine work. Rebase gives a tidier log.
Interactive rebase
git rebase -i HEAD~3
The -i flag opens an editor where you can squash commits together, reword messages, or drop commits. Use it to clean up local history.
Warning
Like amend, rebase rewrites history. Only rebase commits that you have not shared yet.
TL;DR
git rebase mainreplays your commits on top of main.- It creates a clean, linear history.
git rebase -ilets you squash and reword commits.- Never rebase commits that others have already pulled.