我已经提交,但没有将文件推送到存储库,而是将文件推送到分支,即abc
。现在我想从此分支abc
中删除它。我怎样才能做到这一点?
答案 0 :(得分:7)
我假设您想要从历史中删除该文件,就好像它从未存在过一样。如果您只是想在将来的提交中删除该文件,请使用git rm my-bad-file.txt
并忽略我的其余答案。
如果文件仅在您最近的提交中,则最简单的方法是使用git rm my-bad-file.txt
删除文件,然后git commit --amend
编辑之前的提交。
如果有多个提交包含违规文件,那么git filter-branch
可能会有用。
git filter-branch --index-filter 'git rm --cached --ignore-unmatch my-bad-file.txt' master..abc
这意味着:
git filter-branch
:让我们重写一些提交!
--index-filter
:更改每个提交的索引,而不在磁盘上实际检查它。
'git rm --cached --ignore-unmatch my-bad-file.txt'
:对于每次提交,请取消暂存名为" my-bad-file.txt"的文件。如果它存在。
master..abc
:在分支abc的所有提交上执行此操作,返回到从主分支分支的位置。