简体   繁体   English

如何在 git 推力后恢复上次提交

[英]How can I recover last commit after git push force

I have a project repository on bitbucket with master branch.我在 bitbucket 上有一个带有 master 分支的项目存储库。 This branch has some commit that have been committed by my team (the latest commit was not committed by me).这个分支有一些提交是我的团队提交的(最新的提交不是我提交的)。

I just did the git push --force without knowing that it would delete all the commit after the latest pull I did.我只是执行了git push --force并不知道它会在我执行的最新 pull 之后删除所有提交。 So now my team's commit just deleted and the latest commit was the git push --force I just did.所以现在我团队的提交刚刚删除,最新的提交是git push --force我刚刚做的。

Is there a way I could recover my team's commit after I did the git push --force ?在我执行git push --force之后,有没有办法恢复我团队的提交?

I have read some solution and it says by doing git reflog then doing git reset –hard HEAD@{HEAD-NUMBER} .我已经阅读了一些解决方案,它说通过执行git reflog然后执行git reset –hard HEAD@{HEAD-NUMBER} But I'm afraid, could it recover the latest commit or just recover my latest commit (not the latest commit that my team did)?但恐怕,它可以恢复最新的提交还是只恢复我的最新提交(不是我的团队所做的最新提交)?

Git reflog will not help you as you did not pull changes of your teammates and forcefully removed them from remote by pushing your changes. Git reflog 不会帮助你,因为你没有拉你的队友的变化,并通过推送你的变化强行从远程删除他们。

Your teammates who have pushed original commits that you have forcefully removed still have them in the their local branch.推送了您强行删除的原始提交的队友仍然在他们的本地分支中。

they can force push their branch again and overwrite your changes with that what was there before.他们可以强制再次推送他们的分支并用之前的内容覆盖您的更改。 Then you have to fetch their changes and merge them to your branch.然后你必须获取他们的更改并将它们合并到你的分支。

Had you fetched before you force pushed?你在强推之前获取了吗? If so, you should have it here:如果是这样,你应该在这里:

git reflog origin/master

Also, when you force push, it tells you the old and new hash (with an ... or something between) so if you have the console open still, you have the old hash, and might be able to get it.此外,当您强制推送时,它会告诉您旧的和新的 hash(带有...或介于两者之间),因此如果您仍然打开控制台,则您拥有旧的 hash,并且可能能够获得它。

Your team mate that pushed that commit will still have it on their computer if you don't have it on yours.如果您的计算机上没有,您推动该提交的队友仍然会在他们的计算机上。

They will almost always be able to see it like this他们几乎总能看到这样的

git reflog origin/master

And then they can just force push the original hash back然后他们可以强制将原来的 hash 推回去

git push origin [original-hash]:master --force-with-lease

Also in future, always use --force-with-lease , so that you know that you are not deleting something that you haven't fetched... so if you make a mistake, whatever you deleted is on your computer somewhere同样在未来,总是使用--force-with-lease ,这样你就知道你没有删除你没有获取的东西......所以如果你犯了一个错误,你删除的任何东西都会在你的计算机上的某个地方

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM