从修正的提交中删除文件

sa1*_*sa1 11 git version-control

我把提交推到了一个仓库,我不小心添加了一个文件.没有其他人从远程仓库合并,所以我可以重写历史.但是当我从本地提交中删除文件(unstage,而不是从源代码控制或磁盘中删除)时,我无法推送更改.git push显示所有内容都是最新的

Col*_*ert 11

干得好:

git checkout HEAD~ -- path/to/your/file
git add path/to/your/file
git commit --amend -C HEAD
Run Code Online (Sandbox Code Playgroud)
git diff -p HEAD~ -- path/to/your/file | git apply -R
git commit --amend -C HEAD
Run Code Online (Sandbox Code Playgroud)
git reset HEAD~ -- path/to/your/file
git commit --amend -C HEAD
Run Code Online (Sandbox Code Playgroud)


Kur*_*tal 5

尝试:

git rm --cached <yourfile>
git commit --amend
git push -f
Run Code Online (Sandbox Code Playgroud)


Yan*_*aud 5

如果您需要重写完整提交,请尝试使用

git reset HEAD^
git add <files to be part of the commit>
# or git add -pu
git commit -C <previous commit number>
Run Code Online (Sandbox Code Playgroud)

在执行此操作之前,您需要保留最后一个提交编号,以便能够重用提交消息/日期/作者.


sa1*_*sa1 3

虽然我做了类似于 Colin 和 ydroneaud 建议的事情,

答案是使用

git push +sa1:sa1
Run Code Online (Sandbox Code Playgroud)

其中 sa1 是我的分支。这甚至迫使我们推动“无”。