Eugene
Eugene

Reputation: 60184

How can I restore files before a certain commit in GitHub?

How can I restore files before a certain commit in GitHub?

Upvotes: 0

Views: 1188

Answers (1)

Mark Longair
Mark Longair

Reputation: 467191

  1. 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 find some/file.txt back in your working tree, but note that it will also be staged as a change to commit.

  2. 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 do git checkout master.

  3. 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 to tar, as explained in: What's the best way to extract a tree from a git repository?

Upvotes: 2

Related Questions