I create a new file (let's call it myfile.txt) and added some content to it. Then I decided to delete it using git rm
command. Later I realized I shouldn't have deleted the file. Is there a way to get it back? Please note that I haven't added or commited the file.
Asked
Active
Viewed 192 times
-1

Dmitry
- 4,143
- 10
- 46
- 57
-
Possible duplicate of [How to revert a "git rm -r ."?](https://stackoverflow.com/questions/2125710/how-to-revert-a-git-rm-r) – Daniel Feb 02 '18 at 22:59
-
1You didn't `git add` or `commit` the file? – OneCricketeer Feb 02 '18 at 23:02
-
@Daniel that one from my understanding refers to previously commmited file where in my case I haven't added or commited the file – Dmitry Feb 02 '18 at 23:03
-
@cricket_007 no I didn't – Dmitry Feb 02 '18 at 23:03
-
As far as I know, `git rm` does not delete files. It only untracks them. Which, you didn't track them, so therefore, it's not clear how the file was removed – OneCricketeer Feb 02 '18 at 23:10
-
1@cricket_007 By default, `git rm` both untracks and deletes files. It's the first line in the docs "*git-rm - Remove files from the working tree and from the index*". If you only want to untrack it's `git rm --cached`. – Schwern Feb 02 '18 at 23:11
-
@Schwern Hmm. Maybe it's in my git config not to delete, because I just tried it, and the file was not deleted – OneCricketeer Feb 02 '18 at 23:12
-
3@Dmitry If you never `git add`ed the file, then you should not have been able to `git rm` it. You should have gotten `fatal: pathspec 'myfile.txt' did not match any files`. If you did `git add` it, you should have gotten something like `error: the following file has changes staged in the index: myfile.txt`. Can you show exactly what you actually did? Perhaps by running `history`? – Schwern Feb 02 '18 at 23:15
1 Answers
0
It confuses your question a bit. If you executed git rm on the file, it means that you previously followed it. It can help you see the log of that file specifically to be sure:
git log --follow -- myfile.txt
You can use the last commit of the log to restore it
git checkout <commit-sha-1> -- myfile.txt
however, if you never committed the file it is impossible to recover it, at least with Git
I hope that helps

Manuel Ortiz
- 593
- 5
- 11