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

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay