A simple example using branch and merge

Buck Hodges

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: 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: 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

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


Leave a comment

Feedback usabilla icon