Project: Cleaning Commit History with Interactive Rebase
When developing a feature locally, you might make many small, temporary commits (such as "fix typo", "test again"). Before pushing your branch to production, you should clean up your history by merging these commits into a single, clean commit.
1. Interactive Rebase Commands
Use the -i (interactive) flag to modify your commit history:
# Open interactive rebase editor for the last 3 commits
git rebase -i HEAD~3This opens a text editor listing your recent commits, with action options:
pick: Keep the commit as-is.reword: Keep the commit but edit its message.squash: Merge this commit's changes into the previous commit.
2. Walkthrough Guide
Step 1: Create Multiple Small Commits
Make three commits in your demo folder:
echo "line 1" >> log.txt && git commit -am "feat: setup log structure"
echo "line 2" >> log.txt && git commit -am "wip: test typo"
echo "line 3" >> log.txt && git commit -am "fix typo log"Check your log history:
git log --onelineOutput:
abc3333 fix typo log
abc2222 wip: test typo
abc1111 feat: setup log structureStep 2: Open the Interactive Rebase Editor
Run the command to open the rebase editor:
git rebase -i HEAD~3Step 3: Configure Commit Actions
In the editor, modify the action keywords for the commits:
pick abc1111 feat: setup log structure
squash abc2222 wip: test typo
squash abc3333 fix typo logSave and close the file.
Step 4: Write the Final Commit Message
A second editor window will open, asking you to combine the commit messages. Delete the temporary messages and write a clean description:
feat: setup log structure and append linesSave and close the file.
Step 5: Verify the History
Check your log history again:
git log --onelineOutput:
def4444 feat: setup log structure and append linesYour three temporary commits have been squashed into a single commit with a clean history.