git恢复本地文件

背景

项目管理过程中,有时候会误删文件,此时使用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.
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值