Git rebase interactive
Git Rebase Interactive
git rebase -i lets you rewrite your local commit history BEFORE PUSHING.
Use it to:
- clean up messy commits,
- squash “wip” commits
- edit messages.
Basic Usage
To rewrite the last 3 commits:
# HEAD~3 means “the last 3 commits from the current branch.”
git rebase -i HEAD~3Git will open an editor showing something like:
pick a1b2c3 commit message 1
pick d4e5f6 commit message 2
pick g7h8i9 commit message 3 (latest commit)Common Commands You Can Use
"Squash third-commit into second-commit":
pick a1b2c3 first-commit
pick d4e5f6 second-commit
squash g7h8i9 third-commitHow To Cancel
If things go wrong:
git rebase --abortThis returns your branch to its previous state.
Conflict
If conflicts appear, resolve them, then run:
git add .
git rebase --continue