简体   繁体   English

如何将分支合并回主分支并避免树冲突-TortoiseSVN

[英]How to merge branch back to main branch and avoid tree conflicts - TortoiseSVN

I have source files managed in SVN using TortoiseSVN. 我使用TortoiseSVN在SVN中管理源文件。 I had added files and committed them to a revision, and subsequently decided to branch. 我添加了文件并将其提交给修订版,随后决定分支。 I branched with the files I needed, and performed a delete on the trunk of the files that wasn't intended to be used. 我分支了所需的文件,并在不打算使用的文件的主干上执行了删除操作。

Now I'm attempting to reintegrate the branch to the main trunk. 现在,我试图将分支重新整合到主干中。 Using tortoise, I have merged from trunk to branch the range of revisions from AFTER the delete on the trunk to head. 使用乌龟,我已经从树干合并到分支的修订范围,从删除树干到头部。 This brings the branch up to date. 这使分支保持最新状态。

Now I switch to the trunk, and attempt to merge the revisions from the branch to the trunk, and the merge indicates new files being "skipped" or the deletion of my files which are now complete. 现在,我切换到主干,并尝试将分支中的修订合并到主干,并且合并指示新文件正在“跳过”或删除了现已完成的文件。

Am I missing something on the merging to being the trunk up to date? 我在合并中是否缺少最新的东西?

Subversion merges require a few things: Subversion合并需要一些注意事项:

Common ancestry 共同血统

You think this is a no brainer, but this happens quite a bit. 您认为这毫无疑问,但这确实发生了很多。 I've seen developers who would create a branch using svn mkdir , check out that branch, copy the files from the trunk into that branch, and then do a svn add to add them back in. 我见过开发人员会使用svn mkdir创建一个分支,检出该分支,将文件从主干复制到该分支,然后执行svn add来添加它们。

Although the branch and the trunk had the same names and same structure, Subversion looked at them as two completely separate files. 尽管分支和主干具有相同的名称和相同的结构,但是Subversion将它们视为两个完全独立的文件。 There's no common history between the trunk and that branch. 在主干和该分支之间没有共同的历史记录。 Of course, the developer should have used svn cp to copy the trunk to the branch. 当然,开发人员应该使用svn cp将中继复制到分支。

However, you can have issues like this in smaller chunks. 但是,在较小的块中可能会遇到类似的问题。 Imagine a developer makes a branch the correct way. 想象一下开发人员以正确的方式创建分支。 Then discovers that some *.jpg files are missing from the project. 然后发现项目中缺少某些*.jpg文件。 The developer checks out the branch, and adds the files. 开发人员签出分支,并添加文件。 All taken care off. 所有采取了照顾。 Now, the developer realizes that the same bug is also on trunk. 现在,开发人员意识到相同的错误也存在。 No problem, checkout trunk add add the missing *.jpg files. 没问题,结帐中继添加添加缺少的*.jpg文件。

Of course, the *.jpg files on trunk don't share a common ancestry as the ones on the branch. 当然,主干上的*.jpg文件与分支上​​的文件没有相同的血统。 The developer should have merged the revision where those *.jpgs were created from the branch to the trunk. 开发人员应该已经合并了从分支到主干创建那些*.jpgs的版本。

Revision Collisions 修订冲突

Subversion doesn't merge branches: It merges changes. Subversion不合并分支:它合并更改。 This is a tricky concept to understand, but it gives Subversion a lot of merging power. 这是一个很难理解的概念,但它赋予Subversion许多合并能力。

I have a branch I created off of trunk on revision 100. 我有一个在修订版100的主干上创建的分支。

Revision history of branch 分公司的修订历史

  • 100: Created branch 100:创建分支
  • 103: Bugfix 2001 103:错误修正2001
  • 110: Merged trunk to branch 110:将分支合并到分支
  • 112: Made more changes 112:进行更多更改

Revision history of trunk 行李箱的修订历史

  • 102: Changes 102:变化
  • 104: Bugfix 2001 104:错误修正2001
  • 111: Changes 111:变化

Now I want to merge my branch back to my trunk. 现在,我想将分支合并回我的主干。 If I don't specify revisions to use, Subversion tries to figure out which revisions I need to merge. 如果我没有指定要使用的修订,Subversion会尝试找出我需要合并的修订。 I've never merged my branch to my trunk, so Subversion sees that last common ancestor between my branch and trunk is revision 100. It then sees that I need to merge changes 103, 110, and 112 back to my trunk. 我从未将分支合并到主干中,因此Subversion看到分支和主干之间的最后一个共同祖先是修订版100。然后,我需要将更改103、110和112合并回主干。

However Revision 110 on my branch are the changes I've already merged to my trunk! 但是我分支上的修订版110是我已经合并到主干的更改! If I am not careful, I'll be attempting to merge those changes back into my trunk causing conflicts. 如果我不小心,我将尝试将这些更改重新合并到我的主干中,从而引起冲突。

Subversion is suppose to handle this, but not always so cleanly. Subversion可以解决这个问题,但并不总是那么干净。 Before running your merge, do the following command: 在运行合并之前,请执行以下命令:

$ svn mergeinfo --showrevs eligible $URL/branches/branch

This will show the revisions that Subversion wants to merge to my trunk. 这将显示Subversion想要合并到我的主干的修订。 I should look over that list and make sure those changes are not already on my trunk. 我应该查看该列表,并确保那些更改尚未在我的后备箱中。

Let's say I took a look at this list, and realize that this eligible list contains revision 103, 110, and 112. Wait a second. 假设我查看了此列表,并意识到此合格列表包含修订版103、110和112。请稍等。 Revision 103 fixes Bug 2001, but that's already been fixed on trunk. 修订版103修复了Bug 2001,但已在树干上修复。 For some reason, Revision 110 is also listed, and Revision 110 is a merge of my trunk to my branch. 由于某些原因,修订版110也会列出,而修订版110是我的主干到分支的合并。 I don't want this revision to be considered either. 我也不想考虑此修订。

What I need to do is to inform Subversion not to consider these revisions: 我需要做的是通知Subversion不要考虑这些修订:

$ svn merge --record-only -c 103 -c 110 $URL/branches/branch
$ svn commit -m"Updating merge information to prevent collisions with 103 and 110"

Now, I run svn mergeinfo --showrevs eligible again, these two revisions won't be listed. 现在,我再次运行svn mergeinfo --showrevs eligible ,这两个修订版将不会列出。 I've basically informed Subverison that these two sets of changes are already in my trunk. 我基本上已经通知Subverison,这两组更改已经在我的行李箱中了。

You can always use --dry-run to try out a merge before it is complete. 您可以始终使用--dry-run尝试完成合并。 Subversion merging always works if you specify the revisions you want to merge. 如果您指定要合并的修订,则Subversion合并始终有效。 Problems tend to happen when you try to get Subversion to track merging on its own. 当您尝试让Subversion自行跟踪合并时,往往会出现问题。 It's definitely better than it use to be, but Subversion can be confused with complicated circumstances. 它肯定比以前要好,但是Subversion可能会与复杂的情况混淆。 We had a project where branches were renamed, the trunk was replaced, and merging was done between trunk and two other branches. 我们有一个项目,其中分支被重命名,主干被替换,主干与另外两个分支之间进行了合并。 (Don't ask why.). (不要问为什么。)。

The developer was unable to do a merge because they ended up with a few hundred conflicts. 开发人员无法合并,因为最终导致数百个冲突。 Looking at the eligible revisions, I was able to clean up the mess, and get the merge working. 查看合格的修订版,我能够清理混乱,并使合并正常工作。

Remember to merge early and often: The more often you merge, the more likely the merge will work and any conflicts will be easy to find. 请记住要早点合并,并且要经常合并:合并的次数越多,合并的可能性就越大,并且容易发现任何冲突。 Try to keep merging activity simple. 尝试使合并活动保持简单。 If you fix a bug on two different branches, you need to let Subversion know via svn merge --record-only . 如果您在两个不同的分支上修复了错误,则需要通过svn merge --record-only让Subversion知道。

Whatever you do. 不管你做什么。 Don't panic. 不要惊慌 Conflicts will happen, and if you understand how to solve them, you can avoid merge hell. 冲突将会发生,如果您了解如何解决冲突,则可以避免合并。

In order to do a reintegration, you need to merge ALL revisions from Trunk that occurred after you branched into the branch you're looking to reintegrate. 为了进行重新集成,您需要合并从Trunk分支到要重新集成的分支之后发生的所有Trunk版本。

Below is an example: 下面是一个示例:

  • Prev revs ... 上一版...
  • Rev 20 - ( Trunk ) Commit new files Rev 20-Trunk )提交新文件
  • Rev 21 - ( Feature Branch ) Create feature branch 修订版21-功能分支 )创建功能分支
  • Rev 22 - ( Trunk ) Delete files from Rev 20 Rev 22-Trunk )从Rev 20删除文件
  • Rev 23 - ( Trunk ) Work on random files Rev 23-Trunk )处理随机文件
  • Rev 24 - ( Feature Branch ) Work on other random files Rev 24-功能分支 )在其他随机文件上工作
  • Rev 25 - ( Feature Branch ) Merge revisions 22 and 23 into Feature Branch 修订版25-功能分支 )将修订版2223合并到功能分支中
  • Rev 26 - ( Trunk ) Reintegrate Feature Branch into Trunk 修订版26-中继 )将功能分支重新整合到中继中

You'll note that we included the deletion of your files ( Rev 22 ) as part of the merge to Feature Branch ( Rev 25 ). 您会注意到,作为合并到Feature Branch(版本25 )的一部分,我们包括了文件的删除(版本22 )。

Now this creates a new problem for you: 现在,这为您带来了一个新问题:

Your files that you desire to have in your Feature Branch will be deleted when you merge in changes from Trunk during Rev 25 . Rev 25期间合并来自Trunk更改时,您希望包含在Feature Branch将被删除。 This is true and accurate regarding what actually happened in the Trunk . 对于Trunk实际发生的事情,这是正确的。 You will have several options here: 您将在此处有几种选择:

  1. If the files that were deleted in Trunk were modified in Feature Branch during Rev 24 , you'll receive a Tree Conflict when attempting to merge as per Rev 25 . 如果在Rev 24期间在Feature Branch中修改了在Trunk中删除的文件,尝试按照Rev 25合并时会发生树冲突。 You can simply mark the conflict as resolved using your local changes when performing the merge. 您只需在执行合并时使用本地更改将冲突标记为已解决即可。
  2. Perform a record-only merge of Rev 22 into Feature Branch and then subsequently perform any other merges necessary into Feature Branch (this should be equivalent to what I mentioned in #1) Rev 22进行仅记录的合并到Feature Branch ,然后再进行其他必要的合并到Feature Branch (这应该与我在#1中提到的等同)
  3. Perform the merge into Feature Branch as per Rev 25 . 根据Rev 25执行合并到Feature Branch中的操作。 When the merge is complete, you can view the log of Feature Branch and revert the changes made in Rev 25 (the merge itself) which deleted your files by cherry picking the files and reverting each one. 合并完成后,您可以查看Feature Branch的日志并还原在Rev 25 (合并本身)中所做的更改,该更改通过挑选文件并还原每个文件来删除文件。 These reversions will restore the files as new to be added to the repository of Feature Branch . 这些版本会将文件还原为新文件,以添加到Feature Branch的存储库中。 Note that these files will NOT be the same in the history as the original files. 请注意,这些文件在历史记录中将与原始文件不同。 These are considered brand new files as far as SVN is concerned so they do not share any ancestry with their original counterparts . 就SVN而言,这些文件被视为全新文件,因此它们与原始文件没有任何血统

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

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