1

我正在阅读《Git Pro》一书,它说:

要从 Git 中删除文件,您必须将其从跟踪的文件中删除(更准确地说,将其从暂存区域中删除)然后提交。该git rm命令会执行此操作,并且还会从您的工作目录中删除该文件,这样您下次就不会将其视为未跟踪的文件。

下一段谈到git rm -f

如果您已修改文件并将其添加到暂存区域-f,则必须使用该选项强制删除。这是一项安全功能,可防止意外删除尚未记录在快照中且无法从 Git 恢复的数据。

我不明白的是在这两段中,他们都在谈论从同一区域删除文件(我猜是暂存git,在之后添加屏幕截图的地方git add .)。

如果这两个命令都用于从暂存区删除文件,那么有什么区别?

4

1 回答 1

3

git rm -f(或--force)覆盖最新检查。如果没有-f提供,git rm将拒绝删除自上次提交以来修改过的文件,但git rm -f会继续删除。

$ git init
Initialized empty Git repository in /home/ibug/test/.git/
$ touch foo
$ git add foo
$ git rm foo
error: the following file has changes staged in the index:
    foo
(use --cached to keep the file, or -f to force removal)
$ git commit --message "Test foo"
[master (root-commit) 1234567] Test foo
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 foo
$ git rm foo
rm 'foo'
$
于 2018-05-15T01:49:45.843 回答