2

I realised I accidentally edited a file I wasn't meant to a few commits ago. I want to revert this file to the original on master. To that affect I have tried:

git checkout master -- path/to/file.ext

git checkout origin/master -- path/to/file.ext

git checkout origin/master path/to/file.ext

git fetch && git checkout origin/master path/to/file.ext

But for each I've gotten the error "did not match any file(s) known to git." (this file does exist on master).

Would anyone know what I am doing wrong here?

MeltingDog
  • 14,310
  • 43
  • 165
  • 295

2 Answers2

2

Your first attempt looks the closest:

git checkout master -- path/to/file.ext

I don't know whether this even be valid, but in any case you should be checking out from four commits before the current master. Try the following:

git checkout <SHA-1> -- path/to/file.ext

Where <SHA-1> is the hash of the earlier commit in master whose version you want to restore.

If you wanted to undo that entire commit, then a possibly nicer thing to do would be to just revert that entire commit, e.g.

git revert <SHA-1>
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
0

Those should have worked, as I described in "How to get just one file from another branch".
You also have git show commands.

But if you get "did not match any file(s) known to git.", that should mean the file is somehow different from file.ext: possible a lowercase/upercase small difference in the filename or its path.
That or the repo was a shallow clone.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250