首页 > 解决方案 > I accidently deleted everything in a remote repository, and force rewrote history

问题描述

I pushed a commit on a remote repository where I changed one file, but the whole directory of the file was tracked in that commit, so it made it look like I changed every file in this directory.

So, I wanted to erase that commit from the history of the remote repo, with the command git push origin +f2d3f909138e785b63415808c487abc070c4a12b^:master, f2d3f909138e785b63415808c487abc070c4a12b being the commit I wanted to get rid of.

What happened is that every other commits in the history of the remote repo got deleted.

I'd just like to go back to way things were before that last command, the commit is not that much of a problem now. Worse case, I have a copy of the files locally, but the history held a lot of important information still.

标签: gitgithub

解决方案


在您进行强制推进之前,您的团队中应该有一个人最后推进。该人在他/她的本地仓库中拥有所有提交。所以只需要求那个人将最新的提交推送到 origin/master 中:

git push -f origin [commit hash]:refs/heads/master

如果提交是未知的,因为该人获取了最新的更改并覆盖了他的本地分支,那么他可以git reflog用来查找以前签出的提交。

或者,如果您有一个由推送触发的构建服务器(我希望您这样做),您可以在构建日志中找到最新的提交哈希。此外 - 该提交可能仍在 CI 作业的工作目录中。


推荐阅读