How can I restore files before a certain commit in GitHub?
1 Answers
If you want to retrieve an individual file from before the commit
f4l4fe1
, for example, you can do:git checkout f4l4fe1^ -- some/file.txt
(When you add a
^
to a ref in git, it means the parent of that commit.) You should findsome/file.txt
back in your working tree, but note that it will also be staged as a change to commit.If you want to just see the working tree as it was on the previous commit, you can check out the commit as if it were a branch:
git checkout f4l4fe1^
That puts you into a state known as "detached HEAD" where you're no longer on a particular branch, so making new commits won't advance any branch. To get back to
master
, say, you'd just dogit checkout master
.As a third option, suppose you want to extract a whole directory from that commit, or a whole subdirectory, you can use
git archive
and pipe the output totar
, as explained in: What's the best way to extract a tree from a git repository?

- 1
- 1

- 446,582
- 72
- 411
- 327