i accidentally removed file from his repository in github (and i had a lot of changes in some of my files, because i didn't push them yet), and after i remote back to the same repository in github -iv'e added the files again, i made a commit to the files, and when i tried to push i got this error - "fatal: You are not currently on a branch." I tried to "fix" the situation, and made the command - "git rm -r *" and ALL my the content of the file got deleted from my computer. I really dont know what to do now and how do i recover those files back (just a reminder - the current branch on github is not updated to my last commit - because no push was made)
Asked
Active
Viewed 94 times
-2
-
Did you get the "fatal: You are not currently on a branch" error when doing `git commit` or `git push`? I imagine you got it when trying to commit and hence didn't actually commit your changes. – Gabriel Staples Jan 01 '20 at 21:06
-
Also, what did you expect to achieve with `git rm -r *`? – Gabriel Staples Jan 01 '20 at 21:07
-
It was when i tried to do git push. – Noam Atishkin Jan 01 '20 at 21:08
-
@GabrielStaples i searched for an answer online and somehow iv'e seen this solution and just didn't think on the consequences. – Noam Atishkin Jan 01 '20 at 21:09
1 Answers
1
You can get them back with git checkout -- filepath
git checkout -- file1 file2
And so on. They will be checked out as they were on HEAD.

eftshift0
- 26,375
- 3
- 36
- 60
-
even though the i got those messages - "HEAD detached from 'f17603c'"? – Noam Atishkin Jan 01 '20 at 21:11
-
That's fine. A branch is just a pointer to a revision. HEAD is just a (special) pointer to a revision. And you got the error about not being on a branch because you are on **detached HEAD**, that's why git could not know what you wanted to push in the first place. – eftshift0 Jan 01 '20 at 21:14
-
this is what happened after i did the rm command - PS C:\wamp64\www\Dashboard-api\lucy-dashboard-server> git commit -m "your comment" [detached HEAD 27cdf97] your comment 64 files changed, 5703 deletions(-) delete mode 100644 .gitignore delete mode 100644 Procfile and so on to all the rest of the files in that file. – Noam Atishkin Jan 01 '20 at 21:15
-
i got this error when tried to recover file with your command - "error: pathspec 'controllers/contact.js' did not match any file(s) known to git" – Noam Atishkin Jan 01 '20 at 21:24
-
1Oh... sure, the removal is already committed. Try with `git checkout HEAD~ -- file1 file2`. – eftshift0 Jan 01 '20 at 21:33
-