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 ', which is the current default,
  ignores paths you removed from your working tree.

* 'git add --all ' 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 不会 将删除操作提交至暂存区
git add --all 将删除操作提交至暂存区


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

实验代码

# 新建仓库并添加文件
$ 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 ..." 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 ..." 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 ..." to unstage)

    new file:   2.txt
    new file:   3.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:    2.txt

$ 


你可能感兴趣的:(Git,Git,使用)