Ever needed to tweak a commit that’s already in your branch without rewriting history by hand? The combination of git commit –fixup and git rebase –autosquash lets you create targeted fix‑up commits and automatically squash them during an interactive rebase. This short guide shows how to adopt the workflow and boost your Git productivity.
Why use git commit –fixup?
The git commit –fixup flag creates a new commit that is explicitly linked to a previous one. It stores the SHA‑1 of the target commit in the commit message, enabling Git to locate and squash it later. This approach is faster than manually editing, amending, or reordering commits.
Key benefits
- Speed: One‑line command generates a fix‑up commit.
- Safety: No need to rewrite history until you’re ready.
- Clarity: The commit message shows the relationship, helping reviewers understand the intent.
Preparing your repository
Before you start, ensure your local branch is up to date and you have a clean working tree. The workflow works best when you have not yet pushed the target commit to a shared remote, but it can still be used on public branches if you coordinate with teammates.
- Fetch the latest changes:
git fetch --all - Switch to the branch you want to clean:
git checkout feature/awesome - Verify the commit you need to fix:
git log --oneline
Creating a fix‑up commit
Identify the SHA of the commit that requires a change. Then run the fix‑up command, passing the SHA as an argument. Git automatically opens your editor with the staged changes.
- Make the code adjustment you need.
- Stage the change:
git add -p(orgit add .for the whole file). - Create the fix‑up commit:
git commit --fixup=abc1234(replaceabc1234with the real SHA).
The new commit message will look like fixup! Original commit title, which is the exact pattern git rebase --autosquash looks for.
Running an autosquash rebase
Once you have one or more fix‑up commits, initiate an interactive rebase with the --autosquash flag. Git will automatically reorder and mark the fix‑up commits to be squashed into their targets.
- Start the rebase from the commit before the earliest fix‑up:
git rebase -i --autosquash HEAD~5(adjust the range as needed). - The editor opens with a list where each
pickline for a fix‑up is already changed tof(orfixup). - Save and close the editor. Git rewrites the history, squashing the fix‑up commits into their originals.
If conflicts arise, resolve them, stage the resolution, and continue with git rebase --continue. After the rebase finishes, run git log --oneline to verify the clean history.
Common pitfalls and how to avoid them
- Missing target SHA: Double‑check the commit ID before running
--fixup. A typo creates an unrelated fix‑up. - Rebasing too far back: Rebasing beyond the point where the target commit exists will leave the fix‑up unattached.
- Public branches: If you have already pushed the target commit, coordinate with your team or use
git push --force-with-leasecarefully.
Best practices for a clean Git history
- Use
git commit --fixupfor any post‑commit change instead ofgit commit --amendon the fly. - Run
git rebase -i --autosquashas part of your pre‑merge checklist. - Keep the range of the interactive rebase small (e.g., last 10 commits) to limit accidental rewrites.
- Automate the workflow with an alias:
git config --global alias.fx 'commit --fixup'andgit config --global alias.rs 'rebase -i --autosquash'.
Integrating the workflow into CI/CD pipelines
Many teams enforce a linear history on the main branch. Adding a script that runs git rebase -i --autosquash before merge can prevent stray fix‑up commits from entering the shared repository. For example, a pre‑push Git hook could detect fixup! messages and abort the push, reminding developers to squash first.
Conclusion
By mastering git commit –fixup and git rebase –autosquash, you gain a reliable, low‑overhead method for correcting past commits without messy manual rebases. Adopt the step‑by‑step workflow, add helpful aliases, and keep your project history clean and review‑ready.