git: git add --ignore-removal & git add --all 区别

遇到的问题

在仓库中删除文件后,试图直接用 git add . 将所有删除工作提交暂存区,结果遇到了报错:

$ git add .

warning: You ran 'git add' with neither '-A (--all)' or '--ignore-removal',
whose behaviour will change in Git 2.0 with respect to paths you removed.
Paths like '.idea/Wood_Tool.iml' that are
removed from your working tree are ignored with this version of Git.

* 'git add --ignore-removal <pathspec>', which is the current default,
  ignores paths you removed from your working tree.

* 'git add --all <pathspec>' will let you also record the removals.

Run 'git status' to check the paths you removed from your working tree.

经过上网查阅,用 git add --all 解决了问题。

进一步探究

指令区别
git add --ignore-removal <pathspec>不会 将删除操作提交至暂存区
git add --all <pathspec>将删除操作提交至暂存区


很明显我们需要的是第二种。

实验代码

# 新建仓库并添加文件
$ git init
Initialized empty Git repository in /media/hok/test/.git/
$ git add .
$ git status
On branch master

Initial commit

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)

    new file:   1.txt
    new file:   2.txt
    new file:   3.txt

# 删除文件后尝试用 git add --all
$ rm 1.txt 
$ git add --all
$ git status
On branch master

Initial commit

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)

    new file:   2.txt
    new file:   3.txt

# 删除文件后尝试用 git add . --ignore-removal,则删除操作 依然未被 提交至暂存区
$ rm 2.txt 
$ git add . --ignore-removal
$ git status
On branch master

Initial commit

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)

    new file:   2.txt
    new file:   3.txt

Changes not staged for commit:
  (use "git add/rm <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

    deleted:    2.txt

$ 


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值