Developers often run into feature branches filled with commits labeled wip, fix typo, or asdf—names that offer little insight into the actual work. Before sending code for review, organizing these commits into a logical, readable order is key to showing the real development process. The official Git documentation describes interactive rebase as a way to replay a chosen set of commits onto a new base, letting users change both the order and content of those commits. This approach proves especially useful in classrooms and group projects, where a clear commit history helps with peer review and sharing knowledge.
Why Clean Commit History Matters
Readable commit history lets team members see what changed and why. When the log fills up with unrelated fixes or temporary notes, tracking a feature's evolution or debugging later becomes a challenge. Clean history speeds up code reviews and helps maintainers handle merges and releases. The Git Book points out that interactive rebasing is often used to tidy up a local feature branch before merging it into a shared project, making changes easier for maintainers and educators to follow.
Using Interactive Rebase to Restructure Commits
With interactive rebase, you can rewrite recent commit history by combining, reordering, editing, or removing commits. Start by running git rebase -i HEAD~N, where N is the number of recent commits you want to adjust. For example, if you want to clean up five recent commits, use git rebase -i HEAD~5.
Git opens your default editor with a list of commits, oldest first. Each line starts with a command like pick, followed by the commit hash and message. The pick command keeps the commit unchanged. To merge several commits into one, change pick to squash or fixup for the commits you want to combine with the previous one. The squash action merges the selected commit with the one above and opens an editor for a combined commit message. fixup merges the commit but drops its message. This difference helps educators and students learn good version control habits by encouraging clear commit messages and logical grouping. The interactive rebase command set also includes reword, edit, exec, break, drop, and merge, giving flexibility for editing history (Git SCM Documentation).
pick 3d66ed5 Add login form squash 3e5db2d wip squash 4ffd51f fix typo squash fc5d50d asdf pick 2c27ca8 Add login tests
After saving and closing the editor, Git replays the commits and asks you to edit the final commit message for the squashed changes. The end result is a cleaner history, such as one commit for "Add login form" and another for "Add login tests." Commit hashes change after a rebase because Git creates new commits based on your edits. Each Git commit gets a hash from its content and metadata, including parent reference, author, timestamp, and message. Changing the message or parent alters the commit’s identity, so rewritten commits usually get new object identifiers even if the file snapshots stay the same (Git commit documentation).
Automating Fixes with --fixup and --autosquash
If you spot a mistake in an earlier commit, use git commit --fixup=<commit-hash> to create a fixup commit that Git links to the target commit. Later, running git rebase -i --autosquash HEAD~N moves and marks these fixup commits for you, cutting down on manual edits. Setting git config --global rebase.autoSquash true makes this the default for all interactive rebases. The --autosquash feature finds commits with fixup! or squash! in the subject and rearranges the instruction list so the fixup merges with its target, streamlining work for both solo learners and teams (Git SCM Documentation).
Essential Safety Practices
- Only rewrite commits that have not been shared with others. Avoid rebasing shared branches like main after others have pulled them. Git’s documentation warns that rebasing changes the commit ancestry and can cause divergence if applied to commits others may have based work on. The best practice is to clean up a private feature branch before publication and avoid uncoordinated rewrites of shared branches (Pro Git).
- If you have already pushed your feature branch, use git push --force-with-lease instead of --force to prevent overwriting others' work. The --force-with-lease option checks that the remote reference still matches the expected state before replacing it, helping prevent accidental data loss in collaborative educational or professional environments (Git push documentation).
- If you encounter conflicts or need to stop mid-rebase, git rebase --abort will restore your branch to its previous state.
- Should you need to recover previous commits after a rebase, git reflog provides a log of recent branch states. Git retains references to prior positions through its reflog, which records updates to local references such as branch tips and HEAD, offering a recovery point after an accidental or unwanted rebase (Git reflog documentation).
Quick Reference for Interactive Rebase
- git rebase -i HEAD~N: Edit the last N commits interactively.
- squash or fixup: Merge commits into the one above.
- reword: Change a commit message.
- drop: Remove a commit from history.
- git commit --fixup= and git rebase -i --autosquash: Automate fixup merges.
- --force-with-lease, --abort, reflog: Safety and recovery commands.
For a broader look at rewriting and organizing commits, see this earlier breakdown of interactive rebase techniques.
