简体   繁体   English

Git - 如何恢复已推送到远程分支(不是原点)的变基

[英]Git - How to revert a rebase that has been pushed to the remote branch (not origin)

I was working on my local branch doing some changes and when I finished I pushed everything to the remote brach.我正在我的本地分支上做一些更改,完成后我将所有内容推送到远程分支。 Before merging the branch with develop I thought I should do a rebase cos the other guys had merged a lot of their code there.在将分支与 develop 合并之前,我认为我应该做一个 rebase cos,因为其他人已经在那里合并了很多他们的代码。 When I did the rebase and resolved some conflicts I pushed to the remote branch.当我进行 rebase 并解决了一些冲突时,我推送到了远程分支。 Unfortunately the way I resolved the conflicts was wrong so now I need to go back before the rebase happened and also update the remote branch to the new state.不幸的是,我解决冲突的方式是错误的,所以现在我需要在 rebase 发生之前返回并将远程分支更新到新状态。

What I tried我试过的

  1. Reset the head重置头部

    git reset --hard HEAD@{x} //where x is the head just before the rebase git reset --hard HEAD@{x} //这里x是rebase之前的头部

This works and reverts the changes on my local branch but then I don't know what to make the remote branch update to that since it doesn't create a new commit that can be pushed to the remote.这有效并恢复了我本地分支上的更改,但是我不知道如何使远程分支更新到该分支,因为它不会创建可以推送到远程的新提交。

You should not rewrite the history of remote repositories because if 您不应该重写远程存储库的历史记录,因为如果

  1. You push something broken 你把事情推破了
  2. Somebody pulls 有人拉
  3. You force-push a different history 你强行推动不同的历史

not only would you have the issue of having to fix the mess but everybody else who pulled the changes. 你不仅会遇到必须解决问题的问题,而且还有其他推动变革的人。 So unless you can be sure that nobody pulled it, don't do a force-push . 因此,除非你能确定没有人拉动它, 否则不要强行推动

Instead you should revert the commit using either 相反,你应该使用其中之一恢复提交

>> git revert HEAD@{y}   # where HEAD@{y} is the faulty commit

if only one commit was messy, in case of a merge. 如果只有一个提交是混乱的,在合并的情况下。

In case of a rebase that transplatend several commits onto the master branch you need to do 如果有一个rebase将几个提交转换到master分支,你需要做

>> git revert --no-commit HEAD
>> git revert --no-commit HEAD~1
>> git revert --no-commit HEAD~2
   ...
>> git revert --no-commit HEAD@{x}
>> git commit -m "Sorry folks for the big mess I made"

Where all the HEAD~y are the commits in between HEAD@{x} and HEAD . 所有HEAD~y都是HEAD@{x}HEAD之间的提交。

This will effectively undo all affected commits in a single big commit. 这将在一次大型提交中有效地撤消所有受影响的提交。

Dont panic!不要惊慌! just follow these steps in terminal只需在终端中按照这些步骤操作

git reflog 

or或者

git log -g

This will show previous commits in terminal like这将在终端中显示以前的提交,例如

在此处输入图片说明

here commits are named as HEAD@{0}, HEAD@{1} etc. if you want to revert lastest changes than in attached pic HEAD@{5}, but it varies according to your commits这里的提交被命名为 HEAD@{0}、HEAD@{1} 等。如果你想恢复比附加图片 HEAD@{5} 中的最新更改,但它会根据你的提交而变化

git revert HEAD@{5}

Its done.完成。 Now you see all files as before commits现在您可以像提交之前一样看到所有文件

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

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