Branching and merging
Published 16 J 2015
Many development projects involve creating branches (or "forks") for a feature, release, or other development milestone. A branch is essentially a copy of the code base that shares its history. Incorporating the changes from a branch is known as merging.
Development projects typically have:
- a trunk, the main code base, and branches which diverge from it
- one or more branches, copies that diverge from the trunk
For more information about the concepts behind branching and merging, see:
- Branching and Merging with Team Foundation Server (msdn.microsoft.com)
- Branching and Merging with Subversion (svnbook.red-bean.com)
For a general introduction to source control concepts, we recommend Version Control by Example (free PDF) by SourceGear founder Eric Sink.
Working with branches in SQL Source Control
SQL Source Control lets you work with branches, but it doesn't currently let you create or merge branches.
To branch with SQL Source Control, you must create the branch using your source control system and then link to the appropriate branch. Once you've created a branch, you're ready to work on it in SQL Server Management Studio with SQL Source Control.
There are two approaches to working with branches:
Unlink and relink the database
Here, you continue working on the same database in SQL Server Management Studio, but link it to the branch in your source control system.
Once the branch is created, un-link the database in SQL Source Control, then link the database to source control again. When you link, specify the location of the branch in source control.
Create a new database for the branch
This method only works if you're using the dedicated development model.
Here, you create the branch in your source control system, then create a new database to link with it in SQL Server Management Studio.
Create a new empty database, and link it to source control in SQL Source Control. When you link, specify the location of the branch in source control, then on the Get Latest tab, update the database with the latest version from source control.
Merging
SQL Source Control doesn't provide automatic or line-by-line merging. You can use SQL Source Control or SQL Compare to merge at an object level, but not choose line-by-line changes.
When you merge with SQL Source Control or SQL Compare, you choose a version of each object to keep. For example, you might keep the trunk version of a table and the branch version of a view.
There are three approaches to merging:
Merging using your source control system
You can manually merge the branch changes back into the trunk using your source control system as you would for application code.
We recommend this approach if the merge is complex, or if there are conflicts; for example, if the same object has been modified in both the branch and the trunk.
When merging manually, make sure referential integrity is maintained or the database may be left in an invalid state.
Your source control system may include auto-merging functionality that simplifies manual line-by-line merges.
Merging using SQL Source Control
If you don't need to do a line-by-line merge, you can merge with SQL Source Control.
To merge branch changes back into the trunk with SQL Source Control:
- In SQL Source Control, make sure you have a database linked to the branch in your source control repository.
- Get the latest version and commit any outstanding changes.
- Unlink the database from the branch.
- Relink the database to the trunk.
- Go to the Commit Changes tab.
The tab shows the changes to the branch as changes to commit.
If there are conflicts, choose Keep mine to override the trunk with the objects from the branch. - Commit the changes.
The trunk is updated with the branch changes.
Merging using SQL Compare
If there are no conflicting changes between the branch and the trunk, you can merge automatically using SQL Compare.
To merge branch changes into the trunk with SQL Compare:
- In SQL Source Control, make sure you have a database linked to the trunk.
- Use your source control system to create a local copy of the latest branch version, for example by performing an SVN checkout.
- In SQL Compare, create a new project. Set the local copy of the branch as the source, and the trunk database as the target.
For more information, see Setting data sources. - Compare the data sources.
SQL Compare shows the differences between the branch and the trunk.
- In the Results pane, select the objects from the branch that you want to merge into the trunk, and run the Deployment Wizard to deploy from the data source to the target.
The trunk is updated with the changes from the branch. - In SQL Source Control, on the Commit Changes tab, commit the trunk changes to source control.