如何从git push -force中恢复? [英] How to recover from a git push -force?

查看:2824
本文介绍了如何从git push -force中恢复?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里是发生了什么:

我有两个远程git分支: master 特征1 。出于某种原因,我必须为 feature1 分支使用 git push --force ,但我不知道何时我使用 git push --force 它也会推送 master 分支。然后,发生了一场灾难,因为我将本地 master 分支推送到远程存储库。

幸运的是,我的本地分支离遥控器不太远。基本上,我的远程 master 在我的本地 master 之前合并了两个pull请求。



所以我的问题是:我可以重新打开拉请求和remerge?我注意到有合并请求的提交版本,所以我很担心,如果我只是提出新的请求,它会弄乱一些东西?理想情况下,我只想重做两次请求的合并。



是否有其他方法可以从此次灾难中恢复?我了解到 - force 是一个非常糟糕的选择。 :(b / b)

更新,发生的例子:

我有以下分支:

  master 
feature1
origin / master
origin / feature1

我使用GitHub的 Auto merge pull requests 来集成两个pull请求,然后我没有在我的本地机器上获取 master 分支。因此,我认为我的 origin / master 是远程主机的两个版本。

然后我不小心使用了 git -f push ,它覆盖了远程分支,现在我失去了提交如何在不影响其他贡献者历史的情况下从中恢复?

解决方案

通过重置旧提交并发布另一个,您可以始终恢复先前观察到的 master push -f 。涉及的步骤通常如下所示:

 #在本地主设备上工作
git checkout master

#重置到之前的原始状态/主设备, reflog
git reset --hard origin / master @ {1}

#此时确认这确实是所需的提交。
#(如果有必要,使用git reflog找到正确的,
#git reset --hard到那个)

#最后,推动主分支(和只有主分支)到服务器
git push -f origin master

请注意,这会将远程 master 恢复到最近由 git fetch 或等价物检索到的状态。上次提取后,其他人推送的任何提交都将丢失。但是,这些提交仍然可以在他们的 reflog中使用,因此他们可以使用上述步骤恢复它们。


Here is what happened:

I have two remote git branches: master and feature1. For some reason I have to use git push --force for the feature1 branch, but I didn't know when I use git push --force it will also push the master branch. Then, a disaster happened, as I pushed my local master branch to the remote repository.

Luckily, my local branch is not too far away from the remote. Basically, my remote master has two pull requests merged ahead of my local master.

So my problem is: can I reopen the pull request and remerge? I noticed that there is commit version for merge request, so I am worried if I simply make new pull request it will mess up something? Ideally I just want to redo the merging of the two requests.

Is there some other way to recover from this disaster? I learned the --force is a really, really bad choice. :(

Update, example of what happened:

I have following branches:

master
feature1
origin/master
origin/feature1

I integrate two pull requests by using the GitHub's Auto merge pull requests. Then, I didn't fetch the master branch on my local machine. Thus, I think my origin/master is two versions behind the remote master.

Then I accidentally used git -f push, which overwrote the remote branch and now I lost the commits from the pull requests on remote repository.

How can I recover from it without messing up other contributors' history?

解决方案

You can always restore the previously observed state of master, by resetting to the old commit and issuing another push -f. The steps involved typically look like this:

# work on local master
git checkout master

# reset to the previous state of origin/master, as recorded by reflog
git reset --hard origin/master@{1}

# at this point verify that this is indeed the desired commit.
# (if necessary, use git reflog to find the right one, and
# git reset --hard to that one)

# finally, push the master branch (and only the master branch) to the server
git push -f origin master

Note, however, that this restores remote master to the state most recently retrieved by git fetch or equivalent. Any commits pushed by others after the last time you fetched will be lost. However, those commits will still be available in their reflogs, so they can restore them using steps like the above.

这篇关于如何从git push -force中恢复?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆