Interactive Rebasing
Interactive rebasing allows you to edit, reorder, combine, or remove commits before sharing them. This gives
you complete control to clean up your commit history.
Use Cases:
- Squashing multiple small commits into one meaningful commit
- Reordering commits to create a logical history
- Editing commit messages for clarity
- Removing unwanted commits
Examples:
1. Start by creating an index.txt file, initialize it, and paste in the following:
My Delicious Website
Commit the change:
git add .
git commit -m "initial commit"
git commit -m "initial commit"
2. Create feature branch:
git checkout -b feature-pizza-page
3. Add the following to the file:
Pizza is good
Commit the change:
git add .
git commit -m "add pizza page"
git commit -m "add pizza page"
4. Change to:
Pizza is grate
Commit the change:
git add .
git commit -m "I love pizza"
git commit -m "I love pizza"
5. Fix typo to:
Pizza is great
Commit the change:
git add .
git commit -m "I cant spell today"
git commit -m "I cant spell today"
6. Add:
Toppings: peperoni
Commit the change:
git add .
git commit -m "ahskhahjakhsjkdhsa"
git commit -m "ahskhahjakhsjkdhsa"
7. Fix to:
Toppings: pepperoni, mushrooms
Commit the change:
git add .
git commit -m "YES PINEAPPLE FIGHT ME"
git commit -m "YES PINEAPPLE FIGHT ME"
8. View the messy history:
git log --oneline
9. Start interactive rebase:
git rebase -i HEAD~5
10. Change all commits except first to "fixup":
pick abc1234 add pizza page
fixup def5678 I love pizza
fixup ghi9012 I cant spell today
fixup jkl3456 ahskhahjakhsjkdhsa
fixup mno7890 YES PINEAPPLE FIGHT ME
fixup def5678 I love pizza
fixup ghi9012 I cant spell today
fixup jkl3456 ahskhahjakhsjkdhsa
fixup mno7890 YES PINEAPPLE FIGHT ME
***HINT: To save the changes and exit the VS Code Text editor, press ESC, type :wq, and hit ENTER.***
11. View clean history - 5 commits became 1!
git log --oneline
Commands for Interactive Rebasing:
Interactive rebase last 3 commits:
git rebase -i HEAD~3
Interactive rebase all commits on current branch:
git rebase -i main
Continue after editing:
git rebase --continue
Abort if something goes wrong:
git rebase --abort
Keep commit as-is:
pick
Change commit message:
reword
Stop to modify commit:
edit
Combine with previous commit, keep both messages:
squash
Combine with previous commit, discard this message:
fixup
Remove commit:
drop