简体   繁体   English

Git:如何在合并前返回

[英]Git: how to go back before merge

Thanks for your help. 谢谢你的帮助。 I am really confused... 我真的很困惑......

I merged a pull request for a team mate. 我合并了队友的拉请求。

Now I wish to go back to the situation before the merge, because I have realized that bugs are introduced. 现在我想回到合并之前的情况,因为我已经意识到引入了错误。 The pull request was done by a friend on the 24, I did the merge on the 24. 拉请求是由朋友在24上完成的,我在24上做了合并。

  • I need to go back to the situation of the 20. 我需要回到20的情况。
  • what will happen to the changes done by the team mate? 团队成员所做的改变会发生什么? Will I be able to see/use them again? 我能再次看到/使用它们吗?

Thanks!!! 谢谢!!!

commit ff1e9ecfa13c5050957b77bd5b5695d802415867
Merge: 053c894 437e11c
Author: me
Date:   Tue Mar 24 09:06:11 2015 +0100

Merged ' changes on xxx

commit 437e11c23ae7da68f8bb3698557bdc2aa3d9a614
Merge: a129401 adfd8c5
Author: someone else
Date:   Tue Mar 24 00:00:18 2015 +1000

Merge pull request #17 from 

xxx

commit adfd8c5c30541095ee48ce64da14740f92594ec8
Author: someone else
Date:   Mon Mar 23 23:54:06 2015 +1000

xx

commit 053c89426460ffaa86873bed342bc38e24a13096
Merge: 849ec63 35fdbc6
Author: me
Date:   Fri Mar 20 07:57:13 2015 +0100

Assuming you didn't delete the branch on which the original changes appear, then the work from your teammate is still sitting on that branch, so I wouldn't worry about losing those changes so long as you still have that branch where the changes were made. 假设您没有删除原始更改出现的分支,那么您的队友的工作仍然位于该分支上,所以只要您仍然拥有更改所在的分支,我就不会担心会丢失这些更改制作。

You can do a git reset --hard [commit hash] to go back to the commit before the merge, and it's like traveling back in time. 您可以执行git reset --hard [commit hash]以在合并之前返回提交,这就像回到过去一样。 However, you'll also undo any changes that were added after the commit you specify in the reset command, if you have any. 但是,您还将撤消在reset命令中指定的提交添加的任何更改(如果有)。

Ex: 例如:

On my current branch I have the following commits: 在我当前的分支上,我有以下提交:

A
|
B    <- commit *before* the merge
|
|
|  --- some other branch
| /
|/
C    <- commit where "some other branch" was merged into current branch
|
D
|
E

If I do git reset --hard B then I'll go back to the commit before the merge at C . 如果我执行git reset --hard B那么我将在C merge之前返回提交。 But I'll also lose commits D and E in the process. 但是我也会在这个过程中丢失承诺DE Once you do that, just check the state of the branch and make sure it looks the way you need it to, then do a git push --force , which will update the remote repo to this state. 一旦你这样做,只需检查分支的状态,并确保它看起来像你需要它,然后执行git push --force ,它将远程仓库更新为此状态。 The reason you need --force is because you'll be deleting commits D and E from your current branch on the remote repo, and git won't let you do that without the --force option, since you're asking it to delete work. 你需要的原因--force是因为你将从远程仓库的当前分支中删除提交DE ,如果没有--force选项, git将不允许你这样做,因为你要求它删除工作。


Depending on how substantial these changes are, sometimes it's easier to just roll forward and fix the bugs, than to try to undo the changes. 根据这些更改的实质程度,有时更容易前滚并修复错误,而不是尝试撤消更改。 Where that line is (to go forward or back) is a case-by-case decision. 那条线(前进或后退)是个案决定。

Your teammate's commits are in your repository, along with the merge commit you made. 您的队友的提交都在您的存储库中,以及您所做的合并提交。 You can put the labels back where they were before the merge, and if you want to reference your teammate's commits in the future, add a new label. 您可以将标签放回合并之前的位置,如果您希望将来引用您的队友提交,请添加新标签。

Something like this, right? 这样的事,对吧? I think I've got the labels close enough: 我想我的标签足够接近:

   ...053---ff1    master
           /
 ...a17---437      teammate
         /
     ...adf        other

To reset labels, if your most recent checkout wasn't master you just force the label to a new place. 要重置标签,如果您最近的结帐不是master您只需将标签强制到新的位置即可。 Otherwise you have to check out another reference first (or a plain commit) or make the checkout follow along. 否则,您必须先检查另一个引用(或普通提交)或结帐。

You want master back at the first parent of the merge, and having it checked out wouldn't be so bad either, so: 你想让master回到合并的第一个父级,并且检查它也不会那么糟糕,所以:

git tag wip
git checkout -B master master^1

and now master refers to 053 , wip refers to ff1 , and nothing else has changed. 现在master指的是053wip指的是ff1 ,没有别的改变。 The wip reference lets git know you're still referencing it and its history. wip参考让git知道你还在引用它及其历史。 You've got the master commit from before the merge checked out and master is back to referring to that. 你已经在合并签出之前获得了master提交,而master又回到了引用它。 The force-relabel-and-checkout git checkout -B $commit $ref has a shortcut if you're moving your current checkout, git reset --hard $ref . force-relabel-and-checkout git checkout -B $commit $ref有一个快捷方式,如果你正在移动当前的结账, git reset --hard $ref

If you don't have or want master checked out: 如果您没有或希望主签出:

git branch -f master master^1

and that skips all the worktree churn. 并且跳过所有工作树流失。

If you move a label and you realize you really should have tagged the old commit first, you can refer to it through the reflogs, either the label's previous target or your previous checkout, depending: git tag wip @{1} to tag your previous worktree commit and git tag wip master@{1} to tag the previous master commit. 如果您移动标签并且您意识到您确实应该首先标记旧提交,您可以通过reflogs(标签的上一个目标或之前的结帐)来引用它,具体取决于: git tag wip @{1}来标记您之前的标签worktree commit和git tag wip master@{1}标记以前的master提交。 git revisions shows all the ways you can refer to commits, from the number of them you might guess people wind up not bothering with temporary names all that much and just refer to commits used by inflight work directly, and that's exactly right. git revisions显示了你可以引用提交的所有方法,从你可能猜到的人数来看,人们最终不会打扰临时名称而只是直接参考机上工作所使用的提交,这是完全正确的。

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

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