When is git rm -f used?

蹲街弑〆低调 提交于 2019-12-17 19:58:04

问题


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?


回答1:


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




回答2:


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)



回答3:


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


来源:https://stackoverflow.com/questions/8130954/when-is-git-rm-f-used

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