简体   繁体   English

分支后从master删除某些代码时,Git分支并合并

[英]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将执行以下操作:

  1. find the merge base of the current tip of master and the commit to which the name branch-X resolves; 查找当前master提示的合并基础和名称branch-X解析为的提交;
  2. run two git diff s to compare the base to each branch-tip; 运行两个git diff以将基数与每个分支提示进行比较; and
  3. attempt to combine the resulting change-sets, applying this combination to the merge-base content, to come up with the merged content. 尝试合并结果更改集,将此组合应用于基于合并的内容,以得出合并的内容。

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为:

  • Leave yourself (or the person doing the merge) notes. 留下您自己(或进行合并的人)的笔记。 For instance, replace the removed lines with a line that shouts: "PUT THINGS BACK HERE LATER WHEN MERGING." 例如,将替换的行替换为喊叫的行:“稍后合并时将东西放回此处”。
  • Attempt to arrange for a merge conflict , by making sure that whatever commit will be the tip of 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. 它将尝试将YX的修改合并在一起。

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

相关问题 Git分支模型| 合并master和release分支 - Git branching model | Merging master and release branch Git分支:本地创建的分支会自动与本地主分支合并 - Git Branching: Locally created branch is automatically merging with local master branch 合并后保留master分支而不删除代码 - keep master branch without deleted code after merging Git从一个分支分支并随后合并 - Git Branching From A Branch And Subsequent Merging 如何在不与主分支合并的情况下从Git分支测试代码? - How to test code from a Git branch without merging it with master branch? git将master合并到任务分支中正在删除代码 - git merging master into task branch is removing code Xcode 和 git 合并分支到主控后的问题 - Xcode and git problems after merging branch in to master 有繁重的分支被删除而不合并回master时,git repo会累积大小吗? - does git repo accumulate size when there is heavy branch which is deleted without merging back to master? 将旧分支合并到master时,Git不会应用已删除的文件。 如何告诉Git应用已删除的文件? - Git does not apply deleted files when merging an old branch into the master. How can I tell Git to apply deleted files? git 在从 master 分支后为新分支带来一些“旧”更改 - git bring some "older" changes to a new branch after branching from master
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM