Welcome to MSDN Blogs Sign in | Join | Help

A simple example using branch and merge

Merge is an overloaded word. There is a content merge and then there is a branch merge. A content merge is where you have edited a file and someone checked in a newer file, so the source control system will tell you to do a content merge before checking in the file (if it's interactive, it's a 3-way merge GUI).

A branch merge is migrating changes from one branch to another. If a source branch A has a file rock.cs that had been edited and file scissors.cs that had been deleted, merging these changes into a target branch B would result in a pending edit on file rock.cs and a pending delete on the file scissors.cs in the target.

Here's a simple example that uses the command line.  The biggest differences between this and a “real-world“ example are the number of files involved (you may have thousands) and that you would probably have conflicts for a subset of the files (e.g., the same file was edited both in the source and in the target, resulting in the need to do a 3-way content merge).

The /i option is the shortcut for the "noprompt" option that makes everything non-interactive, as would be needed in batch script. Changes are committed in change sets, and the whole check in either succeeds or fails (it's atomic).

1. Create a workspace and add the files mentioned in the merge explanation above.

D:\project>h workspace /i /new MyProject

D:\project>h add /r A
A

A:
rock.cs
scissors.cs

D:\project>h checkin /i
add A

A:
add rock.cs
add scissors.cs
Change set #4 checked in.
2. Create a branch from A to a target B.
D:\project>h branch A B
B

B:
rock.cs
scissors.cs

D:\project>h checkin /i
branch B

B:
branch rock.cs
branch scissors.cs
Change set #5 checked in.
3. Now we have A and B in the repository, related by B being a branch of A.
D:\project>h dir /r
$/:
$A
$B

$/A:
rock.cs
scissors.cs

$/B:
rock.cs
scissors.cs

6 item(s)
4. The branches command shows that B is branched from A.
D:\project>h branches A
>> $/A <<
     $/B Branched from version 1
5. Now we edit rock.cs and delete scissors.cs in the source branch.
D:\project>h edit A\rock.cs
A:
rock.cs

D:\project>echo new line >> A\rock.cs

D:\project>h delete A\scissors.cs
A:
scissors.cs

D:\project>h checkin /i
A:
edit rock.cs
delete scissors.cs
Change set #6 checked in.
6. Merge the changes made in branch A over to branch B. Of course in a real project, there may be conflicts to resolve (rock.cs may have been edited both in the source and target, which will need to be merged with a 3-way content merge), and the developer may need to make additional changes in B (for example, B may be a newer release of the project and the code may be need to be changed due to an API change made earlier in B).
D:\project>h merge A B
merge, edit: $/A/rock.cs;C5~C6 -> $/B/rock.cs;C5
merge, delete: $/A/scissors.cs;C5~C6 -> $/B/scissors.cs;C5

D:\project>h checkin /i
B:
merge, edit rock.cs
merge, delete scissors.cs
Change set #7 checked in.
6. In the history, we see that B\rock.cs was created by branching in change set 5 and then an edit was merged in change set 7.
D:\project>h history /i B\rock.cs
Vers Date       Chngset User          Change Type          Files
---- ---------- ------- ------------- -------------------- --------------------
   2 06/14/2004       7 buckh         merge, edit          $/B/rock.cs
   1 06/14/2004       5 buckh         branch               $/B/rock.cs
Published Monday, June 14, 2004 5:12 PM by buckh
Filed under:

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# Branching, Merging and Unit Testing

Monday, June 14, 2004 5:55 PM by Rob Caron's Blog

# Buck's posts on branching and merging

Tuesday, June 29, 2004 1:45 PM by Chris Rathjen

# Branching and Merging in the Dec. CTP

Monday, January 24, 2005 9:09 AM by Buck Hodges

# Some more merge examples

Thursday, January 27, 2005 4:29 PM by Chris Rathjen

# Some more merge examples

Thursday, January 27, 2005 4:30 PM by Chris Rathjen

# A Brief SourceSafe User's Guide to Team Foundation Version Control

Moving from one software application to another often results in various &quot;surprises&quot; where the new application...
Friday, June 02, 2006 10:23 AM by Ed Hintz (MSFT)

# TechEd 2008 FAQ: Branching and Merging with Team Foundation Server

This was the most popular question asked at our booth (and perhaps all of the VSTS area) at Tech Ed Developers

Wednesday, June 18, 2008 3:57 PM by Chris Rathjen

Leave a Comment

(required) 
required 
(required) 
 
Page view tracker