You can restore deleted files from a commit and then apply a git fixup to update the changes of that commit instead

If you accidentally deleted a file in a commit and want to restore it while updating that same commit, you can use git’s fixup functionality rather than rewriting history manually.

The steps:

  1. Restore the deleted file from the previous commit:

    git checkout [commit_hash]^ -- [file_path]
    

    The caret (^) points to the parent commit where the file still existed.

  2. Create a fixup commit:

    git commit --fixup [commit_hash]
    
  3. Rebase with autosquash to merge the fixup:

    git rebase --autosquash --interactive origin/master
    

    This automatically applies the fixup commit to the original commit.

  4. Force push to your branch:

    git push origin [your_branch] --force-with-lease
    

For the most recent commit, there’s a simpler approach:

git reset HEAD~1  # Undo the last commit
# Restore the file so it's included
git commit -c ORIG_HEAD  # Recommit with the same message
git push --force-with-lease

Using --fixup preserves all other changes from the original commit while fixing the file deletion cleanly.

Original source