git rebase
Rebasing replays your branch's commits on top of another branch, creating a cleaner, linear history instead of a merge commit. It rewrites commit history, so it should be used carefully, especially on shared branches.
A common workflow is rebasing a feature branch onto the latest main before merging, so the history reads as if the feature was built starting from the newest code.
git switch feature-branch
git rebase mainBasic rebase
Run `git rebase main` while on your feature branch to replay its commits on top of the latest main branch commits.
Rebase vs merge
Merge preserves exact history with a merge commit. Rebase rewrites history into a straight line, which looks cleaner but changes commit hashes.
git switch feature-login
git rebase mainSuccessfully rebased and updated refs/heads/feature-login.Replays feature-login's commits on top of the latest main branch.
git rebase --abortCancels an in-progress rebase and returns the branch to its state before the rebase started.
Key points
- Rebase replays commits on top of another branch for a linear history.
- Rebase rewrites commit hashes, unlike merge.
- Never rebase commits that have already been pushed and shared with others.
- git rebase --abort cancels a rebase in progress.
