Streamline fixing previous commits with git commit –fixup and git rebase –autosquash

User avatar placeholder
Written by Tamzid Ahmed

June 22, 2026

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.

  1. Fetch the latest changes: git fetch --all
  2. Switch to the branch you want to clean: git checkout feature/awesome
  3. 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.

  1. Make the code adjustment you need.
  2. Stage the change: git add -p (or git add . for the whole file).
  3. Create the fix‑up commit: git commit --fixup=abc1234 (replace abc1234 with 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.

  1. Start the rebase from the commit before the earliest fix‑up: git rebase -i --autosquash HEAD~5 (adjust the range as needed).
  2. The editor opens with a list where each pick line for a fix‑up is already changed to f (or fixup).
  3. 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-lease carefully.

Best practices for a clean Git history

  1. Use git commit --fixup for any post‑commit change instead of git commit --amend on the fly.
  2. Run git rebase -i --autosquash as part of your pre‑merge checklist.
  3. Keep the range of the interactive rebase small (e.g., last 10 commits) to limit accidental rewrites.
  4. Automate the workflow with an alias: git config --global alias.fx 'commit --fixup' and git 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.

Leave a Comment