Prior to Git 2.23, no.
In Git 2.23 or later, git restore
can do the job:
git restore -s HEAD
Note that git stash -k
does not do the trick: it leaves the working tree matching the index, not the HEAD
commit.1
In all Git versions since 2.5, it may be easier and/or better to use git worktree add
to create a second work-tree based on the current commit:
git worktree add --detach <path> HEAD
where <path>
is wherever you'd like the new work-tree to show up. This has the advantage of not touching your existing work-tree. (In Git versions prior to 2.15, I advise doing whatever you are doing in this added work-tree within two weeks, then deleting it, as there's a nasty little bug in those versions of Git regarding added work-trees. If you're just doing this for the work-tree files, the bug itself is harmless, though.)
In versions of Git prior to 2.23, you can do:
git stash
# do whatever your job is here, followed by `git reset --hard` if needed
git stash apply --index
git stash drop
(or git stash pop --index
; I just like to keep the apply and drop separate myself).
1git stash -k
makes the two stash commits in the usual way, so they are the same as always. But then, instead of git reset --hard
, which a regular non--k
git stash
does, it forcibly adjusts the work-tree to match the stash commit.
The point of git stash -k
is to allow you to run some sort of test that uses the work-tree content, without having to extract the stashed index to another work-tree. For instance, if you have an automated test system that uses what's in the work-tree, rather than what's in the index, you can git stash -k
, run the tests, then git reset --hard
to make the stash applicable again and apply and drop the stash.
Annoyingly, since git stash
doesn't make a stash if the index and work-tree match HEAD
, git stash -k
is hard to use to do this kind of automated testing, because there's no guarantee that it actually made a stash.