【问题标题】:When is git rm -f used?什么时候使用 git rm -f?
【发布时间】:2011-12-29 04:17:53
【问题描述】:

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

【问题讨论】:

    标签: git git-rm


    【解决方案1】:

    解释:

    -f 用于在文件与您上次签出的提交不是最新的情况下删除文件。这是为了防止您删除已更改但尚未签入的文件。


    示例:

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

    【讨论】:

    • 知道了!感谢您详细解释。
    【解决方案2】:

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

    $ ls 函数c $ vim 函数.c ...编辑文件...

    现在想来,其实我是想删掉的……

    $ git rm func.c 错误:“func.c”有本地修改 (使用 --cached 保留文件,或 -f 强制删除) $ git rm -f func.c

    【讨论】:

      【解决方案3】:

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-12-01
        • 2015-08-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-07-20
        • 1970-01-01
        相关资源
        最近更新 更多