First, let me quote a bit of the git stash
documentation:
DISCUSSION
A stash entry is represented as a commit whose tree records the state of the working directory, and its first parent is the commit at HEAD
when the entry was created. The tree of the second parent records the state of the index when the entry is made, and it is made a child of the HEAD commit. The ancestry graph looks like this:
.----W
/ /
-----H----I
where H
is the HEAD
commit, I
is a commit that records the state of the index, and W
is a commit that records the state of the working tree.
The stash
ref (refs/stash
, or in your case, stash@{2}
) then points the W
commit. I prefer to draw this as:
...--G--H <-- branch (HEAD)
|\
i-w <-- refs/stash
myself (showing the setup immediately after your git stash push
command completes successfully), but the information here is the same.
With that in mind: If you're quite certain you want your current working tree copy overwritten with the copy of the file in the W
commit of stash@{2}
, you can simply run:
git checkout stash@{2} -- path/to/file.ext
or:
git restore -SW --source=stash@{2} path/to/file.ext
Note that depending on your shell, the stash@{2}
expression may need some kind of quoting, e.g., --source='stash@{2}'
.
Note also that you can view this version of the file with:
git show stash@{2}:path/to/file.ext
before taking any other action.