不要恢复提交然后推送,因为大文件仍然会保留在历史记录中。
鉴于您尚未推送它并且您要重做的提交是最新的,请从历史记录中删除该提交:
$ git reset HEAD^
这将使您的索引返回到父提交时的状态(HEAD^
)。现在你有了一个调度:按照你第一次的方式添加和提交。
如果您进行了其他后续提交,则需要git rebase -i <commit>
where <commit>
是错误提交父级的 SHA-1。
例如(请注意,您的存储库中的 SHA-1 会有所不同)
$ git rebase -i 57d0b28
会将您带到类似于的编辑器中
pick 366eca1 This has a huge file
pick d975b30 delete foo
pick 121802a delete bar
# Rebase 57d0b28..121802a onto 57d0b28
#
# Commands:
# p, pick = use commit
# r, reword = use commit, but edit the commit message
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
#
# If you remove a line here THAT COMMIT WILL BE LOST.
# However, if you remove everything, the rebase will be aborted.
#
Replace pick
with edit
面临沉重的承诺
edit 366eca1 This has a huge file
pick d975b30 delete foo
pick 121802a delete bar
保存并退出编辑器以返回 shell,您将在其中看到以下形式的消息
Stopped at 366eca1... This has a huge file
You can amend the commit now, with
git commit --amend
Once you are satisfied with your changes, run
git rebase --continue
从那里删除有问题的文件(--cached
仅从索引中删除文件)
$ git rm --cached big-nasty-file
rm 'big-nasty-file'
修改提交
$ git commit --amend
并完成变基
$ git rebase --continue