SQL Source Control 4

Branching and merging

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:

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

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:

  1. In SQL Source Control, make sure you have a database linked to the branch in your source control repository.
  2. Get the latest version and commit any outstanding changes.
  3. Unlink the database from the branch.
  4. Relink the database to the trunk.
  5. 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.
  6. 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:

  1. In SQL Source Control, make sure you have a database linked to the trunk.
  2. Use your source control system to create a local copy of the latest branch version, for example by performing an SVN checkout.
  3. 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.
  4. Compare the data sources.

    SQL Compare shows the differences between the branch and the trunk.

  5. 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.
  6. In SQL Source Control, on the Commit Changes tab, commit the trunk changes to source control.

Didn't find what you were looking for?