DEV Community

Harshit Luthra
Harshit Luthra

Posted on • Originally published at harshit.cloud

Git Interactive Rebase: Clean Up Your Commit History

Originally published at harshit.cloud on 2024-12-19.


TIL: Git Interactive Rebase: Clean Up Your Commit History

Discovered git rebase -i today. Turns five "fix typo" commits into one clean commit before the PR review notices.

the problem

My commit history looked like this:

fix typo
WIP
fix typo again
actually fixed it
remove console.log
Enter fullscreen mode Exit fullscreen mode

Not exactly professional for a PR review.

the solution

Interactive rebase lets you edit, squash, and reorder commits:

# Rebase last 5 commits
git rebase -i HEAD~5

# Or rebase everything since branching from main
git rebase -i main
Enter fullscreen mode Exit fullscreen mode

This opens your editor with:

pick 1a2b3c4 fix typo
pick 5d6e7f8 WIP
pick 9g0h1i2 fix typo again
pick 3j4k5l6 actually fixed it
pick 7m8n9o0 remove console.log

# Commands:
# p, pick = use commit
# r, reword = use commit, but edit message
# e, edit = use commit, but stop for amending
# s, squash = meld into previous commit
# f, fixup = like squash, but discard commit message
# d, drop = remove commit
Enter fullscreen mode Exit fullscreen mode

my workflow

Change it to:

pick 1a2b3c4 fix typo
fixup 5d6e7f8 WIP
fixup 9g0h1i2 fix typo again
fixup 3j4k5l6 actually fixed it
fixup 7m8n9o0 remove console.log
Enter fullscreen mode Exit fullscreen mode

Result: one clean commit.

pro tips

Reword commit messages:

pick 1a2b3c4 fix typo
reword 5d6e7f8 add user authentication
Enter fullscreen mode Exit fullscreen mode

Reorder commits:

pick 3j4k5l6 add tests
pick 1a2b3c4 add feature
Enter fullscreen mode Exit fullscreen mode

Split a commit:

edit 1a2b3c4 huge commit with multiple changes
Enter fullscreen mode Exit fullscreen mode

Then:

git reset HEAD^
git add file1.js
git commit -m "feat: add feature A"
git add file2.js
git commit -m "feat: add feature B"
git rebase --continue
Enter fullscreen mode Exit fullscreen mode

warning

Never rebase commits that have been pushed to a shared branch! You'll rewrite history and cause conflicts for your team.

Safe:

# Your feature branch, not pushed yet
git rebase -i main
Enter fullscreen mode Exit fullscreen mode

Dangerous:

# Main branch that others use
git checkout main
git rebase -i HEAD~5  # DON'T DO THIS!
Enter fullscreen mode Exit fullscreen mode

useful aliases

Add to your ~/.gitconfig:

[alias]
    # Interactive rebase with the given number of latest commits
    rb = "!f() { git rebase -i HEAD~$1; }; f"

    # Rebase on main
    rbm = "!git fetch origin main && git rebase -i origin/main"
Enter fullscreen mode Exit fullscreen mode

Now you can do:

git rb 5        # Rebase last 5 commits
git rbm         # Rebase on main
Enter fullscreen mode Exit fullscreen mode

This has saved me so much embarrassment in code reviews.

Top comments (0)