🔍 Introduction: Why You Need This
Ever looked at your Git history and seen a mess of “WIP”, “fix typo”, and “oops” commits? You’re not alone.
Most developers use git merge and live with cluttered histories. But top engineers at FAANG companies rely on git rebase -i (interactive rebase) to keep their commit logs clean, logical, and professional.
In this guide, you’ll get:
âś… A quick-reference cheat sheet for git rebase -i.
âś… A little-known trick (--autosquash) to automate fixing commits.
âś… Real-world examples you can use immediately.
📜 Git Rebase -i Cheat Sheet
Interactive rebase lets you rewrite, reorder, or combine commits before sharing them. Here’s how to use it:
1. Basic Command
git rebase -i HEAD~3 # Edit the last 3 commits
This opens an editor with your commits and actions you can take:
2. Real-World Example
Problem: You have 5 messy commits:
# Commit list
- Add login button (WIP)
- Fix typo
- Refactor auth logic
- Fix lint error
- Update README
Solution:
git rebase -i HEAD~5
An editor window will open showing the following.
- Squash “Fix typo” and “Fix lint error” into “Refactor auth logic.”
- Reword “Add login button (WIP)” to “Implement login button.”
- Drop unnecessary commits.
For the next window, enter the reworded commit “Implement login button” and hit save.
For upcoming windows, don’t change anything and hit close.
Result: A clean, logical history.
# Commit list
- Refactor auth logic
- Implement login button
- Update README
đź’Ž The Hidden Gem: --autosquash
Manually marking commits for fixup is tedious. Instead, use --autosquash to automate it:
How It Works
1. Mark commits as fixes:
This command adds a prefix fixup! to the commit message.
git commit --fixup=COMMIT_HASH
This is the COMMIT_HASH with which you want to squash the current code.
2. Run rebase with --autosquash:
git rebase --autosquash HEAD~N
When you run above command, it automatically merge fixup commits into original COMMIT_HASH.
Example
# Accidentally left a small change from committing? Fix it:
git add .
git commit --fixup=abcd123 # commitSHA of the original commit
git rebase --autosquash abcd123 # No manual editing!
Why FAANG engineers love this:
- Saves hours of manual rebasing.
- Keeps history clean without effort.
🚀 Pro Tips for Safe Rebasing
⚠️ Golden Rule: Never rebase pushed commits (unless you’re alone on the branch).
đź”§ Recover from mistakes:
Already pushed an incorrect rebase to repo. No worries, use this.
git reflog # Find the commitSHA for pre-state
git reset --hard commitSHA
📌 Alias for speed: Add this to ~/.gitconfig:
[alias]
ri = rebase -i
ras = rebase -i --autosquash
Up Next in the Series: Day 2: git cherry-pick—The Surgical Way to Move Commits.
Daily advance GIT tips in your inbox—worth starting? Respond to my poll here🚀
For more useful and innovative tips and tricks, Let's connect on Medium
Top comments (0)