GIT - Restore a deleted file in a Git repo


/ Published in: Bash
Save to your folder(s)

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.


Copy this code and paste it in your HTML
  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>

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

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.