git 删除和恢复本地删除的文件

 文章参考 http://www.tuicool.com/articles/3Qj6Nj

 

由于习惯问题,会经常直接删除文件,那么现在有两种操作

 

Administrator@XZ-20141030RYHZ /f/git/22 (master)

$ git status

On branch master

Changes not staged for commit:

  (use "git add/rm ..." to update what will be committed)

  (use "git checkout -- ..." to discard changes in working directory)

 

        deleted:    a.txt

        deleted:    bbb/bb.txt

 

no changes added to commit (use "git add" and/or "git commit -a")

 

第一种:该文件不交由 git管理,删除文件

Administrator@XZ-20141030RYHZ /f/git/22 ((ca1a93e...))

$ git rm a.txt

 

Administrator@XZ-20141030RYHZ /f/git/22 (master)

$ git status

On branch master

Changes to be committed:

  (use "git reset HEAD ..." to unstage)

 

        deleted:    a.txt

 

Changes not staged for commit:

  (use "git add/rm ..." to update what will be committed)

  (use "git checkout -- ..." to discard changes in working directory)

 

        deleted:    bbb/bb.txt

 

Administrator@XZ-20141030RYHZ /f/git/22 ((ca1a93e...))

$ git commit -m "delete file a.txt"

 

备注:

     如果想还原git rm a.txt,则可以使用git reset HEAD

或者

     Administrator@XZ-20141030RYHZ /f/git/22 ((ca1a93e...))

     $ git reflog

     ca1a93e HEAD@{0}: reset: moving to HEAD@{1}

     89e9b95 HEAD@{1}: checkout: moving from master to HEAD@{1}

     ca1a93e HEAD@{2}: commit: dsafdsa

     89e9b95 HEAD@{3}: commit (initial): dsaf

     git reset HEAD@{N}//N表示要回退的版本编号

    git rest HEAD  表示HEAD@{0}

 

 

第二种:还原删除的git文件

还原其中的一个文件

git checkout a.txt

 

批量还原所有文件

具体做法 git checkout file 同时恢复多个被删除的文件:

git ls-files -d | xargs -i git checkout {}

备注:如果这条命令起作用,必须保证路径中没有中文

 

如果对这条命令不是很清楚,则参考 http://hbiao68.iteye.com/admin/blogs/2162156 文档

 

你可能感兴趣的:(git 删除和恢复本地删除的文件)