Git: copy from commit to working directory

Viewed 1580

Is there a git command that copies files from the commit history to the working directory without also copying to the staging area? Checkout copies to both. Maybe there isn't a common enough use case for this to warrant a command, I'm asking more as a curiosity than because I have a specific application.

3 Answers

Easiest way to do this is to use git reset and the reflog:

First, apply the filesystem state from the commit (or branch) to your working directory, staging area, and branch.
git reset --hard [commit/branch]

Then reset the staging area and branch to previous state, without modifying the working directory (a '--mixed' reset, which is the default):
git reset HEAD@{1}

The files from that commit are in your working directory and nothing else. Note that if you modify HEAD in between step 1 and step 2 then HEAD@{1} most likely won't be the correct ref, you can use git reflog to find it.

Related