2

I made some changes. Now after several commits and probably merges I want to revert a specific file back to a particular commit state in the workspace.

What is an appropriate git command?

Ann
  • 47
  • 5
  • https://stackoverflow.com/q/215718/7976758 Found in https://stackoverflow.com/search?q=%5Bgit%5D+restore+file+old+commit – phd Apr 10 '23 at 08:22

2 Answers2

3

You can revert a particular commit's state of the file back to the workspace with the following git command:

git checkout <COMMIT#> <relative/path/to/your/file>

Example:

git checkout 22864c16a5647d3b4ccb034aa5698f196a648a38 Gemfile

Roman
  • 19,236
  • 15
  • 93
  • 97
  • 1
    Although `git checkout` is perfectly fine for the job, you can now also mention `git restore` : `git restore -W -s 22864c16a -- path/to/file` – LeGEC Apr 10 '23 at 06:53
0

You can execute:

git checkout 0e97a7045e0b234efe5062600905468430ede879

The aproach above will move the pointer to this commit, but the branch will appears with the name like (HEAD detached at 147e81b7), or you can will to path .git\refs\reads find your branch and change the UUID there for your hash commit. This approach is the better IMO. To a single file you can execute the command line like @Roman sad

git checkout 22864c16a5647d3b4ccb034aa5698f196a648a38 Gemfile
Ivan Rodrigues
  • 441
  • 4
  • 20
  • git checkout 0e97a7045e0b234efe5062600905468430ede879 goes not to the point of the question – Ann Apr 10 '23 at 16:21