Back to roadmaps git Course

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~3

This 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 --oneline

Output:

abc3333 fix typo log
abc2222 wip: test typo
abc1111 feat: setup log structure

Step 2: Open the Interactive Rebase Editor

Run the command to open the rebase editor:

git rebase -i HEAD~3

Step 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 log

Save 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 lines

Save and close the file.

Step 5: Verify the History

Check your log history again:

git log --oneline

Output:

def4444 feat: setup log structure and append lines

Your three temporary commits have been squashed into a single commit with a clean history.

Published on Last updated: