Discard candidate merge changesets

August 30, 2012

Doing parallel development across multiple branches is quite easy by creating a number of branches in TFS, but the difficult part is to setup a valid branching strategy and to enforce best practices like always pushing forward integration actions before the reverse integration operation, …

In some scenarios it will be required to avoid that individual changesets can be merged into another branch (for example configuration changes). Enter discarding candidate merges!

Using the Merge Wizard in TFS, you have the option to select the cherry-pick option to only see the changesets which haven’t been merged from Source to Target.



But, from the UI it’s unfortunately still not possible (TFS 2012) to discard a candidate merge changeset. I would just love to right-click on a changeset and discard it from there.

Up till now I always switched to the command-line to discard the changeset from there (tf merge /discard).


Recently I got a question at a customer to make this possible through the API. First of all I was a bit worried why on earth it would be necessary to automate this through the API because in the end this action should be quite exceptional. So, after going into more details it seemed that a background process was running which made a number of configuration changes to the branches, but those changesets were branch-specific and weren’t allowed to be merged into some other branch. These changesets were in fact seriously blocking the merge process due to the fact that only consecutive changesets can be merged from Source to Target and a number of these “configuration” changesets were sitting between other valid candidate merge changesets. Not good!

The solution for discarding changesets through the API is making use of the Merge method on the Workspace object. To discard, you must use the enumeration AlwaysAcceptMine as parameter for the MergeOptions. This will push the merge (without actual changes in the files) into the workspace after which the check-in can be executed. The check-in will make sure that the changeset won’t show up anymore in the candidate merge changesets.