How do i get a deleted file back when using git repositories?



  • From time to time we all make mistakes like trashing a file we should have trashed. When tracking a file using git repositories, you can get it back.

    Initial git status show everything is clean and good.

    $ git status
    On branch master
    nothing to commit, working directory clean
    

    Let's manually delete a file to simulate a mistake

    $ rm README.md 
    

    Another status command, as git is tracking the file, you see it in status deleted.

    $ git status
    On branch master
    Changes not staged for commit:
      (use "git add/rm <file>..." to update what will be committed)
      (use "git checkout -- <file>..." to discard changes in working directory)
    
    	deleted:    README.md
    
    no changes added to commit (use "git add" and/or "git commit -a")
    

    Let's get it back from the master branch, the last know required commit.

    $ git checkout master README.md
    
    $ ls
    README.md
    
    $ git status
    On branch master
    nothing to commit, working directory clean
    

    We have the file back. If you wanted the file deleted for real, you can got through the git add/git commit process to update the git repository.


Log in to reply
 

© Lightnetics 2024