简体   繁体   English

如何使用 git 从另一个分支获取新更改

[英]How to get the new changes from another branch with git

I merged branch B to my branch C a week ago and did some updates on my branch C.一周前我将分支B合并到我的分支C并在我的分支 C 上进行了一些更新。

The same time, my colleague, did some updates on his branch B .同时,我的同事对他的分支B做了一些更新。

I want to get his updates on my branch C .我想在我的分支C上获得他的更新。

I am really confused, because he tells me to just rebase on his branch - but that keeps deleting all my new updates / added files.我真的很困惑,因为他告诉我只是重新建立在他的分支上-但这会不断删除我所有的新更新/添加的文件。

I tried to merge the two branches again - but somehow I don't get his updates this time .我试图再次合并这两个分支 - 但不知何故我这次没有得到他的更新

Our project looks kind of like this:我们的项目看起来像这样:

Origin---o---o---o---o---o---      main
 \
  o---o---o---o---o---o---o---o---o B
   \               \(merged)       \(rebase here?)
    o---o---o---o---o---o---o---o---o C

Rebasing is complicated, and was probably a bad idea in this situation.变基很复杂,在这种情况下可能是个坏主意。 Let's adopt your topology:让我们采用您的拓扑:

o---o---o---o---C1---o---o---o---o friend
 \               \
  o---o---o---o---M---o---o---o me

To pick up your friend's changes, the right and simplest thing to do was (on me ):要获取您朋友的更改,正确且最简单的做法是(在me ):

git merge friend

That would give this:那会给这个:

o---o---o---o---C1---o---o---o---o friend
 \               \                \
  o---o---o---o---M---o---o---o---M2 me

You cannot coherently rebase at this point, despite what your friend said, because me already includes a merge commit (M) from friend .尽管您的朋友说了什么,但此时您无法连贯地重新设置基准,因为me已经包含了来自friend的合并提交(M)。 Rebasing can't easily cope with that;变基不能轻易解决这个问题; it will ignore the merge commit, and thus the effect of the merge will be undone in your branch.它将忽略合并提交,因此合并的效果将在您的分支中撤消。 There are ways around this, but it's not worth going into them here.有一些方法可以解决这个问题,但在这里不值得讨论。

The key question now, however, is how to get out of the mess you've gotten yourself into with a bad rebase followed by a bad merge.然而,现在的关键问题是,如何摆脱由于糟糕的 rebase 和糟糕的合并而让自己陷入的混乱局面。 The answer is to use the reflog , which makes much of what you do in Git undoable.答案是使用reflog ,这使得您在 Git 中所做的大部分工作都无法撤消。 When you say git reflog you will be shown a list of all previous states of your HEAD.当您说git reflog时,您将看到 HEAD 的所有先前状态的列表。 One of these will be the state just before you started the rebase .其中之一将是 state就在您开始 rebase 之前 You want to go back to that by saying git reset --hard <SHA> (as I explain here ).您想通过说git reset --hard <SHA>将 go 回到那个状态(正如我在这里解释的那样)。 Now do the merge that you should have done, and move on.现在做你应该做的合并,然后继续。

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

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