68

I made something very stupid. I made a commit using git commit (file edits + new files) (C). Then I made amend last commit. Then I deleted all files recursively (!) using git rm -r Then I made another git commit (C).

A-B-C
    ↑
  master

Is there any way to undelete the files but keep the changes I had in my first commit? (C) I'd rather do not go back to (B). I tried git reset --soft head^, so then the git status lists files I deleted, then I did git checkout, but still no luck. I don't even know if it's possible.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Nately
  • 691
  • 1
  • 5
  • 6
  • so you want your files of commit A back? – uday Feb 28 '12 at 07:01
  • 1
    No, I want to still be on C but without files deleted, only my file edits and file adds. I do not have the files in my working tree now, they are deleted. – Nately Feb 28 '12 at 07:05

2 Answers2

187

Do yourself a favour and do not do git checkout <hash> like the other answer suggests and go into more problems.

IF you have deleted file from your working directory and not committed the changes yet, do:

git checkout -f

CAUTION: commit uncommitted files before executing this command, otherwise you're going to lose them all

The deleted files should be back again.

If not and if you can find the commit that you want ( C, etc. - your question is not clear ) from git reflog, just do git reset --hard <hash from reflog> and you should be all set.

slm
  • 15,396
  • 12
  • 109
  • 124
manojlds
  • 290,304
  • 63
  • 469
  • 417
  • 3
    +1 You might add a link to a detached head question (like http://stackoverflow.com/questions/3965676/why-did-git-detach-my-head/3965714#3965714), to illustrate the issue with the git checkout SHA1 answer. Would ORIG_HEAd have helped in this case? (http://stackoverflow.com/questions/964876/head-and-orig-head-in-git/964927#964927) – VonC Feb 28 '12 at 07:44
  • 1
    Ok, so this answer is actually better. reset --hard to leaves me on my master branch, and I'm not left in 'detached HEAD' state. The answer by Sasha was also good in case of retrieving my lost data. – Nately Feb 28 '12 at 08:01
  • 23
    In addition to restoring your deleted file, `git checkout -f` also discards all other changes that you have not staged so beware. – chrisjleu Mar 12 '14 at 15:49
  • 3
    That last comment needs to be highlighted. And beware that it does NOT restrict itself to your current directory as you might assume, it'll clobber your entire repository. wow. – Tom Mar 26 '14 at 11:30
  • 8
    for selective undelete of a specific file: git checkout -- – Marco Faustinelli Aug 21 '14 at 08:33
  • 1
    If the file is not yet commited, but already staged, you need to unstage it first (git reset HEAD ). – Aconcagua Nov 21 '14 at 10:26
  • 1
    i've managed to accidentally delete some untracked files. any solution? – BenKoshy Mar 24 '16 at 00:40
6

If I understood you correctly you rewrote commit C. So the original commit, let's call it C1 is not accessible from your commit graph, but it is still there (git keeps all commits for a while). Use git reflog to get the commit hash and git checkout <hash> or another appropriate command to get to the old state C1.

Sascha
  • 1,104
  • 8
  • 15
  • One more question. Now it says I'm in 'detached HEAD' state. How do I get back to be on my 'master' branch? – Nately Feb 28 '12 at 07:25
  • You are welcome. However, I do not understand the down vote. A git reset --hard is potentially far more dangerous. It was not clear if the poster had amended the original commit C with changes he wanted to keep. – Sascha Feb 28 '12 at 08:09