I have made some changes and staged them all into the index. Now I would like to throw away the working tree, and make it look exactly like the committed HEAD
, without throwing away the index. How can this be achieved?
Everything I've looked at is geared toward the opposite: preserving the working tree while manipulating the index, or else manipulating both.
For instance, is there some way of stashing that index, and then later staging those stashed changes without doing anything to the working tree?
For reference, the following hacky achieves the effect, at least when the materials involved are patchable text files. Assume all changes have been staged with git add
:
# apply the staged diff to the working tree, in reverse; i.e. undo it.
git diff --cached | git apply -R
After this, git diff --cached
continues to show the staged changes, and git diff
shows the reverse diff: those changes being undone relative to the index.
Answers to this question must reproduce the effect of the above command pipeline.