How to clean Git history
A clean Git history makes it easier to understand what changed and why, to git bisect for regressions, and to review pull requests.
As the creator of CoreUI with 25 years of open-source development experience, I review history quality as part of every pull request and use interactive rebase to clean branches before merging.
The primary tool is git rebase -i (interactive rebase), which lets you squash, reword, drop, and reorder commits with a text editor.
The result is a polished history that reads as a coherent narrative rather than a stream of “WIP” and “fix typo” commits.
Open interactive rebase to edit the last N commits.
# Edit the last 5 commits
git rebase -i HEAD~5
# Or edit all commits since branching from main
git rebase -i $(git merge-base HEAD main)
Git opens your editor with a list of commits and available commands:
pick 3a1b2c3 Add user authentication
pick 7d4e5f6 WIP: fix login bug
pick 9g8h7i8 fix typo
pick 2j3k4l5 actually fix login bug
pick 6m7n8o9 Add tests for auth
Common Interactive Rebase Commands
Use these commands to reshape the commit list.
# Available commands:
# pick = keep the commit as-is
# reword = keep commit, edit the message
# squash = meld into the previous commit, combine messages
# fixup = meld into the previous commit, discard message
# drop = remove the commit entirely
# reorder lines to change commit order
pick 3a1b2c3 Add user authentication
reword 7d4e5f6 WIP: fix login bug ← rename to meaningful message
fixup 9g8h7i8 fix typo ← merge typo fix silently
fixup 2j3k4l5 actually fix login bug ← merge fix into the reworded commit
pick 6m7n8o9 Add tests for auth
Saving this file produces two commits instead of five: “Add user authentication” and the reworded login bug fix with its typo and subsequent fix absorbed into it.
Rewording a Commit Message
Change a commit message without altering its content.
git rebase -i HEAD~3
# Change "pick" to "reword" for the commit you want to rename
# Save — Git opens another editor for the new message
Or for the most recent commit only:
git commit --amend
# Opens editor with the current commit message — edit and save
git commit --amend is simpler for fixing the last commit message. For older commits, use reword in interactive rebase.
Dropping a Commit
Remove a commit and its changes entirely from history.
git rebase -i HEAD~5
# Change "pick" to "drop" for the commit to remove
# Or simply delete the line from the editor
Dropping a commit removes it as if it never existed. If later commits depend on its changes, you’ll get a conflict during rebase that you’ll need to resolve manually.
Pushing Cleaned History
Rewriting history requires a force push to update the remote branch.
git push origin feature/my-branch --force-with-lease
--force-with-lease rejects the push if the remote branch has received new commits since your last fetch. This prevents accidentally overwriting a teammate’s work on a shared branch. Never force-push to main or master.
Best Practice Note
This is the same cleanup workflow required for all CoreUI pull requests — interactive rebase before merge keeps the main branch history readable. Run git log --oneline after rebasing to verify the history looks right before pushing. For more extensive cleanup such as removing sensitive data from history, see how to remove sensitive data from Git history. You can also squash all branch commits into one for an even simpler merge.



