Posted By

satie83 on 04/18/13


Tagged

delete files


Versions (?)

GIT - Restore a deleted file in a Git repo


 / Published in: Bash
 

URL: http://stackoverflow.com/questions/953481/restore-a-deleted-file-in-a-git-repo

Say I'm in a Git repository. I delete a file and commit that change. I continue working and make some more commits. Then, I find I need to restore that file.

  1. /* Find the last commit that affected the given path. As the file isn't in the HEAD commit, this commit must have deleted it.*/
  2.  
  3. git rev-list -n 1 HEAD -- <file_path>
  4.  
  5. // Then checkout the version at the commit before
  6.  
  7. git checkout <deleting_commit>^ -- <file_path>
  8.  
  9. ##############################################################################################
  10.  
  11. // Or in one command, if $file is the file in question.
  12.  
  13. git checkout $(git rev-list -n 1 HEAD -- <file_path>)^ -- <file_path>

Report this snippet  

You need to login to post a comment.