[英]Git branching and merging when certain code from master is deleted after branch
I branched X from master, Deleted some code Y from master. 我从master分支了X,从master删除了一些代码Y。 When i merge code from X back to master how do I make it such that Y is added back to master(since it hasn't been deleted from X). 当我将代码从X合并回master时,如何将Y添加回master(因为尚未从X删除)。
If you create a branch, change code in master and merge the (at least in this part) unchanged branch, that code won't come back. 如果创建分支,请在master中更改代码并合并(至少在此部分中)未更改的分支,该代码将不会返回。
Git is about the changes ("commit") made. Git与所做的更改(“提交”)有关。 If you merge a branch which has changed parts at the same place where the master has changed you get a notification and the merge is paused. 如果您在主节点已更改的同一位置合并了已更改部件的分支,则会收到通知,并且合并将暂停。 Otherwise the changes are, well, merged . 否则,更改将被合并 。
When you eventually run git checkout master && git merge branch-X
, Git will: 当您最终运行git checkout master && git merge branch-X
,Git将执行以下操作:
master
and the commit to which the name branch-X
resolves; 查找当前master
提示的合并基础和名称branch-X
解析为的提交; git diff
s to compare the base to each branch-tip; 运行两个git diff
以将基数与每个分支提示进行比较; and 和 If the master
-side change involves deleting some set of lines Y
, that's one of the inputs to step 3. There is no way to avoid it being one of the inputs to step 3. 如果master
端更改涉及删除某些行Y
,则这是步骤3的输入之一。无法避免将其作为步骤3的输入之一。
Unless you use a different merge strategy 1 —merge strategies have total control, hence can modify any or all of the above three steps, or even eliminate some or all of those steps—the only things you can do right now to prepare for disaster during step 3 are: 除非您使用不同的合并策略1-合并策略具有完全控制权,所以可以修改上述三个步骤中的任何一个或全部,甚至消除其中的一些或全部步骤-您现在唯一可以做的就是在灾难期间准备灾难步骤3为:
branch-X
in the future will contain changes to the same set of lines. 尝试安排合并冲突 ,方法是确保将来任何会成为branch-X
尖端的提交都将包含对同一行的更改。 Doing both is going to have the highest probability of success, since whoever does the merge (you or someone else) will see the shouty warning. 同时执行这两项操作的成功几率最高,因为合并的任何人(您或其他人)都会看到大声的警告。 But note that both of these preparation steps might be undone by someone else down the line, some time before the future git merge
. 但是请注意,这两个准备步骤可能在将来的git merge
之前的某个时间被其他人撤消。 There is nothing you can do to prevent that. 您无法采取任何措施来防止这种情况。
1 Whoever actually runs git merge
, in the future, has this control, via git merge -s <strategy-name>
. 1将来实际运行git merge
人都可以通过git merge -s <strategy-name>
来拥有此控件。 However, at the present and in what I consider the foreseeable future, there is no magical merge strategy that would do what you want anyway. 但是,在目前以及我认为可以预见的将来,没有一种神奇的合并策略可以满足您的需求。 So this is little help either. 因此,这也无济于事。
Just run git merge
, that's what this command is for. 只需运行git merge
,这就是该命令的作用。
It will try to combine the modifications from Y
and X
together. 它将尝试将Y
和X
的修改合并在一起。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.