24

Is it possible overwrite a branch with another?

Or is the only solution to delete branch B and make a new branch from Branch A?

Henrik
  • 483
  • 1
  • 4
  • 16

4 Answers4

24

Unless you're running TFS 2010, I'd recommend using Merge + Resolve to bring the two branches back in sync.

# cancel out of conflict dialog
tf merge A B -r -force -version:T
tf resolve B -r -auto:acceptTheirs

That should equalize everything, except for files that were only created in B and never merged back. Use Folder Diff to find & reconcile them.

Delete + rebranch in 2005/2008 runs the risk of nightmarish-to-debug namespace conflicts in the future. The other option, if you have 2008, is to Destroy + rebranch. Obviously that assumes you are ok with losing all the history from the original copy of B.

John Gietzen
  • 48,783
  • 32
  • 145
  • 190
Richard Berg
  • 20,629
  • 2
  • 66
  • 86
  • 3
    "Delete + rebranch in 2005/2008 runs the risk of nightmarish-to-debug namespace conflicts in the future. " - Amen to that! – AakashM Jan 19 '10 at 15:11
  • What did you mean by "Unless you're running TFS 2010"? Would you use something other than the merge and resolve combination that you described here for TFS 2010? Why? – Scott Munro Jul 22 '15 at 14:21
  • Should the value that you are passing to the auto option of the resolve command be TakeTheirs instead of acceptTheirs? This is the value that I found in the documentation. https://msdn.microsoft.com/en-us/library/6yw3tcdy(v=vs.100).aspx – Scott Munro Jul 22 '15 at 15:04
  • Maybe I am wrong, but I would not mark this as a correct answer. The 'force' option simply ignores the history and merge changes even if some of them were merged before. The key point here is that TFS merge is not working based on file content but based on the history. The 'baseless' merge is in fact solving the issue completely. – user2323704 Sep 04 '15 at 14:46
19

In Visual Studio 2010, I just used the baseless merge to achieve this:

Tf merge /baseless [source path] [target path] /recursive

When the resolve conflicts window popup, select 'Take source version' option. Note, the files only exist in target will not be deleted by using baseless merge, but you can compare the 2 branches to identify the difference and delete them manually.

Refer to: http://msdn.microsoft.com/en-us/library/bb668976.aspx

Showching Tong
  • 201
  • 2
  • 3
  • 1
    I think that this should be the correct answer for the problem. As I have written above the 'force' option will not be usefull in all cases, because it still based on the changeset history and not on the file content. – user2323704 Sep 04 '15 at 14:47
9

I had success using Richard Berg's answer but felt like there were a few details that were missing. This is how I was able to eliminate differences related to conflict resolutions and changesets that had not been merged back to the source and make the target identical to the source. Note that this is how I will be referring to the branches - source is the one that should not be changed while target is the one that should be made identical to source.

  1. Make sure that you have the latest version of both the source and target branches in your workspace. Also make sure that you do not have any pending changes.
  2. Discard all of the candidates for merges from the target branch to the source branch. This is to prevent any of these differences from being unintentionally merged back into the source branch later on.

tf merge $/target $/source /recursive /discard

Note: Make sure that you have set the working folder to a path within your workspace so that the tf tool knows which workspace and TFS server to use.

  1. Check that there are differences between the branches that should be addressed - if not, no further action is required.

    3.1. View history on the target branch and find the last complete merge from the source to the target (ignore any cherry picking merges as that will result in lots of false positives) - note the changeset number. An easier alternative would be to perform a merge from the source branch into the target branch to get the lastest version from the source branch into the target branch.

    3.2. Right click on the source branch in the Source Control Explorer and select Compare from the context menu. Only take the latest version for the source if you merged the source branch into the target branch in step 2.1. Otherwise select Changeset from the Source Version | Type combo box and then enter the changeset number from step 2.1.

    3.3. Use the drop down arrow on the Browse button next to the Target Path field to select Server Path. Then select the target branch in the dialog that opens.

enter image description here

3.4. Click OK on the Compare dialog to perform the comparison.

  1. Perform a merge from the source branch to the target branch and pass the force option. This will ignore the merge history and merge changesets even if they have been merged in the past.

tf merge $/source $/target /recursive /force /version:T

Note: the T version specification indicates the latest version.

  1. Close the merge conflicts dialog.

  2. Automatically resolve all conflicts by choosing the source branch.

tf resolve $/target /recursive -auto:TakeTheirs

  1. Check in.

  2. Confirm that there are no longer any candidates for merges from the target to the source and that there are no differences between the two branches (this time just using the latest versions as we did a forced merge of the latest version in step 4.).

See MSDN for more details on the tf merge and resolve commands.

Community
  • 1
  • 1
Scott Munro
  • 13,369
  • 3
  • 74
  • 80
-1

Delete the branch B and create a new one from branch A.

Is there a reason why you do not want to do this?

Martin Woodward
  • 11,770
  • 31
  • 45
  • I want to keep the same name, and I wonder if it might be a problem to delete and rebranch. – Henrik Jan 19 '10 at 14:04
  • No problems that I can think of (apart from the fact that un-deleting the old branch would be difficult if you ever wanted to do that) – Martin Woodward Jan 20 '10 at 16:01
  • This answer was downvoted - I am guessing - because history would be lost. But how important is that history really, if your goal is to make the target branch identical to the source? This is still an option in some scenarios. – Scott Munro Jul 22 '15 at 14:05