遇到的问题
在仓库中删除文件后,试图直接用 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