18

I accidentally removed the entire directory of my source code...with a nice rm -r. I know, really bad; but fortunately, I had a git repo in the containing directory. Thus, git has a huge list of unstaged changes of deleted files. For example:

"deleted:   src/caronmonitor/server.py" 

How do I get these files back? There is advice all over the web to do:

git checkout file

or

git revert <commit>

But as I understand that will restore the file to it's state at the last commit. I don't want to go back to the last commit but instead go back to right before the delete operation. I can look in the gitk and see my files as they were before the delete; thus this must be possible.

3
  • Have you staged the file before deleting it? Otherwise git diff will just show you the state of the last commit
    – knittl
    Jul 23, 2014 at 13:30
  • They weren't staged. I messed up, I mean gitk. So what is in gitk under "Local uncommitted changes, not checked in to index" is what is at the last commit?
    – Vance T
    Jul 23, 2014 at 13:48
  • git checkout -- <filename> or git checkout -- '*.sh' for all .sh files worked for me for changes that were not staged for commit. The meaning of -- is to checkout from file, not the branch. See git-scm.com/docs/git-checkout and mudongliang.github.io/2018/12/23/…
    – AMS
    Jul 20, 2022 at 1:01

6 Answers 6

21

Yes, You should do a git checkout filename

If you have multiple files, and want to restore all of them back, use git checkout . from the root of your git directory.

A checkout will restore the files to last version that was added/committed; if a file hadn't been added or committed yet, than it will be lost.

So, for example:

$ git init && touch test1.txt test2.txt test3.txt
$ git add test1.txt && git commit -m "test1" && git add test2.txt
$ ls -a
.  .. .git test1.txt  test2.txt test3.txt

#deleting files below, 2 will be recovered, 3rd will be gone.
$ rm *
$ ls -a
.  .. .git

$ git checkout .
$ ls -a
.  .. .git test1.txt  test2.txt
#test3.txt is gone, test2.txt was recovered, even though not committed but just added
8

Not, GIT does not do any magic. If you did not staged or commited your changes then those are gone. You can see deletions but you can only revert those to last state that you told GIT to remember it for you.

You have to tell explicitly GIT to remember your changes by staging and commiting.

So if you have file.txt in your repository with content:

    int main(argc[] args){
      System.out.println("example");
    }

This was your last change that was commited or staged.

Now you edit file.txt to contain something like:

int main(argc[] args){
 System.out.println("example");
 System.out.println("Hey I can print more lines");
}

You save your file, close editor, do nothing with git and do rm -r Now the file is there, and git has reference to file and that it was deleted but content of this file in git is only:

    int main(argc[] args){
      System.out.println("example");
    }
2
  • I understand git doesn't do any magic; but it seems that in gitk git has a copy of the files before that delete. I also accidentally deleted a single file from a git repo once and was able to restore it, at least, I thought I did.
    – Vance T
    Jul 23, 2014 at 13:52
  • @Vance-Turner I have added little example to ilustrate how I see this.
    – Mateusz
    Jul 24, 2014 at 6:19
7

Something similar has happened to me. I have used the Android Studio, but it would work in other JetBrains IDE as well.

I have restored my deleted files via Local history > show history and then I have reverted all deleted files.

Hope it helps someone.

1

git won't have stored any changes that were never added or committed. The best you can do is git checkout that deleted directory, and you'll get it back in the state it was in when things were last committed.

git checkout <branch_you_are_in> <directory_of_all_your_source_code>

The local uncommitted changes you are seeing is probably the uncommitted deletion, i.e. git sees that you've deleted a bunch of files, but you haven't committed the fact that they are deleted yet, so git says 'hey, there are some changes (in this case, deletions) you haven't committed.'

1

I had opened the file in Eclipse IDE, so in order to restore the changes that I had lost accidentally by doing git checkout, I did as suggested below in the Eclipse IDE.

https://www.oreilly.com/library/view/eclipse-cookbook/0596007108/ch04s08.html 4.7. Restoring Elements and Files from Local History

You also can revert an entire file to its previous version by right-clicking it in a view such as the Package Explorer and selecting one of the following menu items:

Replace With→ Previous from Local History

Replaces the file with the previous version in local history

Replace With→ Local History

Replaces the file with a version you select from local history

Restore from Local History

Restores a file from local history
0

Turned out that new uncommitted files when removed can be restored from the Recycle Bin(I'm using Sourcetree).

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.