首页 > 解决方案 > git:从已推送到功能分支的远程删除不需要的文件

问题描述

我不小心将一个不需要的文件推送到了我的远程功能分支。

有这个documentation/~$screenshots.pptx文件可能是我打开文档但没有关闭时自动生成的临时文件系统。

在我生成一个 PR 后,我发现这个文件也被推送了。

尝试使用 git rm documentation/~$screenshots.pptx 但收到此错误:

fatal: pathspec './documentation/~.pptx' did not match any files

由于我是我最后一次提交,所以我git reset --soft HEAD^现在使用。
检查git status:documentation/~$screenshots.pptx列为new file: documentation/~$screenshots.pptx.

git rm documentation/~$screenshots.pptx了还是fatal: pathspec './documentation/~.pptx' did not match any files报错。

我怎样才能从我的 PR 中删除这个文件?

标签: git

解决方案


如果文件被列为“新文件:”,这意味着它在索引中,可以提交。

在这种情况下,要使用的命令是:

git rm --cached 'documentation/~\$screenshots.pptx'

(尝试不同的语法,但重点仍然存在:您需要使用--cachedoption

然后提交并推送以更新您的 PR。


推荐阅读