Kirk S.
Kirk S.

Reputation: 151

How to pull some files from an old commit to the current master

I have a repository that belongs only to me but allows me to manage a project across multiple machines with versioning. Somehow, at some point I committed in such a way that some important includes were deleted, but I did not notice until I needed them today. So I am trying to figure out how to take the versions of those files from the last commit where I know they are correct and pull them into the current master. There are a couple of questions on here that seem like they should answer this, but the answers seem to be describing something else.

I can check out from the correct commit and I read about rebasing, but I am hesitant to do anything without asking those with more experience.

Upvotes: 2

Views: 54

Answers (2)

Enrico Campidoglio
Enrico Campidoglio

Reputation: 59963

You can use git checkout to put back the deleted files into your working directory.

Assuming you already know which commit deleted the files, you can say:

git checkout <commit-ish>^ -- <deleted-path>

where:

  • commit-ish is a reference to the commit in question
  • ^ references that commit's parent
  • deleted-path is the path of the file or directory that you want to restore

For example, assuming a12b34 is the SHA-1 of the commit that deleted the foo directory, you would say:

git checkout a12b34^ -- path/to/foo 

In short, this commands tells Git to get the files in the specified path as they were before the commit that deleted them, and put them back in the working directory.

Upvotes: 1

Shravan40
Shravan40

Reputation: 9908

git log --summary --diff-filter=D

Let's say you find out the exact commit hash as abcde12345 where this important file was deleted.

git checkout $commit~1 path/to/file.ext

Where $commit is the value of the commit you've found abcde12345

Upvotes: 1

Related Questions