DEV Community

Benji πŸ™
Benji πŸ™

Posted on

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

  1. git checkout [previous_commit_hash]^ -- [file_path]
    a. The ^just specifies the parent commit of the previous commit hash where the file shouldn't have been deleted

  2. (optional) Edit your file if you want to include any change in the commit instead

  3. git commit --edit --fixup [previous_commit_hash]

  4. Rebase with autosquash so you apply the fixup commit to the previous commit hash git rebase --autosquash --interactive origin/master

  5. Force push (with lease) to your feature branch git push origin [your_branch] --force-with-lease

Any changes in the previous commit are just retained as normal

The other way you can do this (if its the latest commit) is to just git reset HEAD~1 to undo those changes, checkout the file that was deleted so its no longer included, then commit again with the same commit message. You'd still need to force push though since you're rewriting the commit history

Top comments (0)