git恢复本地文件

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u010039418/article/details/86168853

背景

项目管理过程中,有时候会误删文件,此时使用git pull没办法恢复。那有没有什么办法可以像svn那样,把当前缺失的文件一把恢复呢?答案是肯定的。

解决手段

1、恢复单个文件

如果删除的只是某个文件,处理方式很简单,直接checkout即可,

git checkout -- file

其中,checkout后面两个-,是为了防止刚好有个分支名字为file。

如果删除的文件很多呢,总不可能一个个找出来checkout吧。。。

2、恢复多个文件

其实思想倒还真是一个个找出来,只不过git有命令帮我们找而已。

git ls-files -d | xargs git checkout --

使用git ls-files可以查看本地被删除的文件,然后管道交给checkout批量处理。

除了查看删除的文件,还可查看当前修改的文件,我们可以看下git ls-files的帮助信息,

SYNOPSIS
       git ls-files [-z] [-t] [-v]
                       (--[cached|deleted|others|ignored|stage|unmerged|killed|modified])\*
                       (-[c|d|o|i|s|u|k|m])\*
                       [-x <pattern>|--exclude=<pattern>]
                       [-X <file>|--exclude-from=<file>]
                       [--exclude-per-directory=<file>]
                       [--exclude-standard]
                       [--error-unmatch] [--with-tree=<tree-ish>]
                       [--full-name] [--abbrev] [--] [<file>]\*
OPTIONS
       -c, --cached
           Show cached files in the output (default)

       -d, --deleted
           Show deleted files in the output

       -m, --modified
           Show modified files in the output

       -o, --others
           Show other (i.e. untracked) files in the output

       -i, --ignored
           Show only ignored files in the output. When showing files in the
           index, print only those matched by an exclude pattern. When
           showing "other" files, show only those matched by an exclude
           pattern.

猜你喜欢

转载自blog.csdn.net/u010039418/article/details/86168853
今日推荐