0

I committed and pushed changes to my package-lock.json file two commits ago.

How do I uncommit the changes to this specific file?

I've looked at commands like $ git reset HEAD~2 but can't figure out how to target the specific file only.

haydnD
  • 2,225
  • 5
  • 27
  • 61
  • After `git reset HEAD~2`, you can do `git checkout ./path/to/your/package.json` and that should reset the file. – Joshua Oct 21 '22 at 21:56
  • You can't "target the specific file only" while rewriting history. That said, do you actually need to rewrite history and scrub the changes to that file, or do you simply want to revert the file to a previous state? – user229044 Oct 21 '22 at 22:03

1 Answers1

0

I am assuming you committed changes in a file, which should not have been commited, and now you want to rewrite history to undo this.

I would recommend using interactive rebase. Like this:

git rebase --interactive HEAD~3

Then it will give you a file to edit (like commit) where you can choose for each commit (the last three in this case) what to do. Then you chose amend for the commit where you did the stuff you want to erase from history. Then you undo the damage, and put git rebase --continue.

David van rijn
  • 2,108
  • 9
  • 21