Published on

Fixup Commit in Git

Authors

You can ammend a remote commit with a file change in your local repository by running this command:


git --no-pager add <local-file-you-want-to-add-to-a-commit> && \ 
git --no-pager commit --fixup=<commit-hash-you-want-to update> && \ 
git --no-pager rebase -i --autosquash <commit-hash> 

This command sequence will:

  1. Stage the specific file you want to add to the target commit
  2. Create a fixup commit that will be merged into the target commit
  3. Start an interactive rebase that will automatically squash the fixup commit into the target commit

Save the changes and force push.

git push <branch-name> --force-with-lease

If you have local changes you haven't committed yet, the rebase will fail. You can solve this by temporarily stashing your local changes and then running the rebase command like this:

git stash -u && \
git rebase -i --autosquash <commit-hash-you-edited-earlier> && \
git stash pop