Friday, March 23, 2007

Discarding changes in merge

Recently, one merge-related question came up repeatedly on the MSDN newsgroups, and I thought it is worth to dedicate a post to the issue of discarding the merges.

The typical scenario would be this - you branched the code, changed files and upon merge you do not want to merge the actual changes. But you do want to make sure that changeset in question will not appear in the future merges.

The best way to do that is to use discard option of the merge command. The option is available only through command-line client (tf.exe) and basically performs merge without taking any changes from the source to target; its only purpose is to update the merge history between source and target and thus prevent the discarded changeset appearance in the future.

Few tips along the way on using the discard command switch:
1. If you trying to discard specific changeset, it needs to be specified twice, as the command requires that the range of changesets will be specified (the example merges changes in changeset 666 made in file1.cs from branch to trunk):
tf merge $/project/branch/file1.cs $/project/trunk/file1.cs /discard /version:C666~C666

2. If you are merging folders, do not forget the recursive switch (the example merges all changes in changeset 666 made in branch folder to trunk folder):
tf merge $/project/branch $/project/trunk /discard /recursive /version:C666~C666

Overall, this command may come in real handy at times; it is a pity that it's only available through command line.

In conclusion, great thanks to Richard Berg for clarifying some of the above and for his educational efforts on MSDN forums.

Update: Judging by the forums questions, it appears necessary to mention, that after performing merge you are still required to check in the changes; though there is essentially no change in file contents, TFS paradigm requires check in of every change. And if you think of it, the whole flow should not be different from any merge.