Finding merge candidates in TFS

2019-01-28 18:00发布

问题:

I have two branches for my code. Lets say B1 and B2. Code changes happen on both the branches parallely and we do a periodic merges (not everything, only necessary things) from B1 to B2. However, this merge is done manually (for whatever reason) by copy pasting the code from one branch to another. Now, I want to make sure that all the necessary things are merged from B1 to B2. Note that the files in B2 branch may contain additional changes other than coming from B1. I tried using tfs merge /candidates ... command hoping that it will file comparison (may be using diff tool) and give the output, but that doesnt work. Only if I had used tfs merge command it will record the merge. Is there any simple solution where I can remove the false positives if the file is manually merged?

回答1:

You can do a discard merge. This has to be done from the command line. Open up the Developer command prompt, then navigate to a folder under either of your branches (i.e. navigate to one of the affected workspaces). Then type:

tf merge /r /discard "$/Project/B1" "$/Project/B2" /v:C12345~C12345

This will take the changeset identified (in this case it was changeset #12345), and update it as merged to the target branch (branch B2). The target files will be checked out, but they will not be changed - you can simply check them in to complete the operation. After that the changeset will no longer appear as a merge candidate. You can specify a range of changesets to merge at the same time, but they should be contiguous.

Note that after doing this a changeset will occasionally still show up as a merge candidate - this is rather uncommon with the latest versions of TFS, and it is virtually impossible to fix (unless you are running your own local install of TFS and want to get your hands very dirty in the database). If you end up with one of these marooned changesets, just ignore it.

TFS does a fantastic job of merging and helping you resolve merge conflicts. The only time you want to "merge" things manually in the way you are is when you have a lot of auto generated code (which produces a lot of hard to resolve conflicts at merge time) or you have binary files.



回答2:

Doing the manually merge contrary the principle of source control to a certain extent. Strongly recommend you use tf merge command or the Source Control Merge Wizard through VS to achieve the merge. You can merge a changeset version, date version, label version, even a workspace version. If you had used the command, everything will be very easy. The simplest solution just as you mentioned, use tfs merge /candidates which will prints a list of all changesets in the source that have not yet been merged into the destination. You just need to check if you had merge all the necessary things to Branch2.

If you insist on the manually merge, the only possible method to remove the false positives is comparing files, folders or branch after the merge. You can use the Compare folders in VS or quick compare(just click mouse twice) with this extension TFS Productivity Pack (Visual Studio 2015)



回答3:

I dont think there is any simple solution to this situation. In the question you mentioned that you merge things which are only necessary, would it be okay to merge everything from B1 -> B2? If yes, then read on:

  1. If the additional changes in B2 are minimal, first create a list of the files modified in B2 branch (additional code which are not part of B1). Create a new branch from B1 (say B2_new) and checkout/edit the files in the B2_new branch as prepared from the list above. (During check-in you might have conflicts to work through.) But this creates a new baseline and now you know everything from B1 is now present in B2_new. Discard/lock the B2 branch and use B2_new going forward.

  2. If the changes in B2 are significant to the point where it requires a lot of manual editing to move it to B2_new, then I would just suck it up and do a merge from B1 to B2 (full branch or only the folders that you need). Yes this is going to show ton of conflicts even for files which you already merged manually. But to create that merge relationship in TFS you gotta do it. That will be a one big bad merge.

Personally, if the B2 changes are minimal I would prefer option 1 as it is much cleaner and you will get a clean slate to work with. Hope that helps.