I have an old commit that I did a few weeks ago. I want to restore only a single file from that commit. What do I do?
I have an old commit that I did a few weeks ago. I want to restore only a single file from that commit. What do I do?
All answers mention git checkout <tree-ish> -- <pathspec>. As of git v2.23.0 there's a new git restore method which is supposed to assume part of what git checkout was responsible for. See highlights of changes on github blog.
The default behaviour of this command is to restore the state of a working tree with the content coming from the source parameter (which in your case will be a commit hash).
Assuming the commit hash is abcdef the command would look like this:
git restore --source=abcdef file_name
which (by default) puts it in working tree. If you want to put the change directly in index so it can be committed straight away:
git restore --source=abcdef --worktree --staged file_name
or with short option names:
git restore -sabcdef -W -S file_name