When is git rm -f used?

情到浓时终转凉″ 提交于 2019-11-28 11:57:25

Explanation:

The -f is 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.


Example:

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

If you try to git rm a file that has unstaged changes, it fails if you don't provide the -f flag:

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

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
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!