Eric Auld
Eric Auld

Reputation: 1244

Git revert fails to recover removed file

I have a series of commits

A --> B --> A'

The changes in commit B were to execute git rm myfile and add myfile to .gitignore. I then pushed commit B to remote. But I realized I forgot to add the --cached option to git rm, so myfile was actually deleted from my local repository, whereas I just wanted to stop tracking it and delete it from remote.

In order to fix this, I made commit A', which was nothing but a git revert HEAD~, reverting back to A. It successfully reverted back to A. But myfile did not reappear.

If I do git ls-tree A then myfile is there, but if I do git ls-tree A' then myfile is not there. What is going on?

Upvotes: 0

Views: 48

Answers (2)

LeGEC
LeGEC

Reputation: 51850

If you just want to get file myfile from commit A to your disk :

git show A:myfile > myfile

# or :
git restore --worktree -s A -- myfile

Upvotes: 2

eftshift0
eftshift0

Reputation: 30212

I think you wanted to run git revert HEAD to get back the file.

Upvotes: 1

Related Questions