什么时候使用 git rm -f?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8130954/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 06:09:51  来源:igfitidea点击:

When is git rm -f used?

gitgit-rm

提问by Mohnish

I am learning Git and am unable to understand under what condition the -f flag is used while issuing the "git rm" command. Please explain a scenario where rm -f would be required instead of rm only?

我正在学习 Git 并且无法理解在发出“git rm”命令时在什么情况下使用了 -f 标志。请解释需要 rm -f 而不是 rm 的场景?

采纳答案by Andy

Explanation:

解释:

The -fis used to remove a file if the file is not up to date with your last checked out commit. It is to prevent you from removing a file that you have made changes to, but have not yet checked them in.


-f用于删除一个文件,如果该文件是不是最新的你最后检出提交。这是为了防止您删除已更改但尚未签入的文件。


Example:

例子:

You check out commit 0a12d4that contains the file sample.txt. Before you change any files, you could remove the sample.txtwith git rm sample.txt. However, once you make a change to sample.txt, you would need to use git rm -f sample.txtto remove the file

您签出包含文件sample.txt 的提交0a12d4。你改变任何文件之前,你可以删除sample.txt的使用。但是,一旦对sample.txt进行更改,您就需要使用删除文件git rm sample.txtgit rm -f sample.txt

回答by svick

If you try to git rma file that has unstaged changes, it fails if you don't provide the -fflag:

如果您尝试git rm使用具有未暂存更改的文件,如果您不提供-f标志,它将失败:

$ git rm a.txt
error: 'a.txt' has local modifications
(use --cached to keep the file, or -f to force removal)

回答by Dietrich Epp

If you edit a file, and then realize you want to delete it instead.

如果您编辑了一个文件,然后意识到要删除它。

$ ls
func.c
$ vim func.c
...edit the file...

Now that I think about it, I actually want to delete it...

现在想想,我真的想删了……

$ git rm func.c
error: 'func.c' has local modifications
(use --cached to keep the file, or -f to force removal)
$ git rm -f func.c