Working With Multiple Branches in Source Control

September 3rd, 2020

With the help of dbForge Source Control tool that comes as an add-in for Microsoft SQL Server Management Studio and is included in dbForge Studio for SQL Server, you can quickly connect databases you’re working on to your version control system of choice and easily switch between different branches.

This can significantly speed up the database development process by allowing you to version-control the changes in your data and table structure right from SSMS or our IDE. As it is often necessary to work with a few branches in parallel or in close succession, let’s take a look at how this can be done with the help of dbForge Source Control add-in for SSMS.

How Source Control implements interaction with multiple branches

Source Control is used as a client for your chosen version control system. In this article, we provide a worked example of how to quickly connect a database to Git and easily switch between branches with the help of dbForge Source Control. With the tool, you can connect a database to a specific repository retrieved by connecting to a version control system and then select a branch in this repository. After the database is linked to a target repository, you will be able to commit changes to the current repository branch, pull the latest commits from the remote repository, and view change history for any file from this database – all of this can be done right in Microsoft SQL Server Management Studio.
However, you will not be able to create new branches and delete obsolete ones using Source Control. Instead, this should still be done using the standard version control system commands in the command line or through a dedicated app of your preferred VCS.

Linking a database to a version control system

If you want to link a database to a new branch, you will first need to clone the remote repository to a local folder. One of the ways to do this is by installing Git and using standard Git console commands:

  • Create a folder in which you would like to store the local copy of the repository
  • Open the command line and execute the following command:
git clone https://remote_repository_url “local_repository_folder”

Here, you need to replace https://remote_repository_url with the actual URL of your target repository and local_repository_folder with the full path to the folder you created in the previous step.

  • If the process is successful, you will see an output similar to the following in the command line:
Cloning a repository to a local folder

Now that you have a local clone of the repository, we can link a database to a VCS using Source Control.

In Microsoft SQL Server Management Studio, right-click the necessary database in the Object Explorer, go to Source Control, and click Link Database to Source Control:

Select dbForge Source Control add-in

The Link Database to Source Control wizard will open. Here, click the ‘plus’ icon in the Source control repository field:

Link Database to Source Control wizard

The Source Control Repository Properties window will appear. First, select the version control system type in the Source control system field (we’re using Git here). Then, select a folder with your Git repository clone and press OK:

Source Control Repository Properties window

Then, back in the Link Database to Source Control window, select the database development model (we’re choosing Dedicated in this worked example) and click Link:

Selecting developer mode

When the process is complete, a new tab will open in SSMS allowing you to access the Source Control UI and functionality, and the database linked to the version control system will be marked with a blue source control sign in Object Explorer.

Basic Git commands and branches in Source Control

Suppose that in a remote repository there is a database we want to work with. First, we need to create an empty local database and link it to the remote repository in question. Having done that, we’ll see the following:

dbForge Source Control - link a database

To synchronize your dedicated database with Git repository, click Get Latest. After that, your local database will be 100% copy of the database in the repository.
Let’s presume, we need to create a table in the database but we want to work in a separate branch, not in the master one. Thus, we have to create a branch and prepare it for work. To achieve that, we need to execute the following commands:

create a local branch and switch to it

git checkout -b develop

This command checks out a branch called develop  based on master, and the -b flag tells Git to create the branch if it doesn’t already exist.

prepare your new branch for work

git push --set-upstream origin develop

This command pushes the develop  branch to the central repository (origin).

Source Control - cmd git commands

Having created and prepared the branch for work, we can proceed to create a table. After that, click Refresh in Source Control.

SQL Source Control - Refresh

After clicking the Commit button, the changes will be committed to the develop  branch and published on a server.

Source Control for SQL Server - push on server

Let’s say a new developer wants to join the work. He makes the repository clone on his local machine, creates an empty database, links the database to his clone repository, as described earlier in the article.
Having done all this, he will see the following:

Version-control sl databases - link a database to clone repository

As you can see, the current branch is master, and there is no third table in remote changes.
To get the latest changes, our new developer needs to execute the following command:

git checkout develop

This command checks out a branch called develop based on master. As you remember, we created a table in this branch.

Source Control - cmd git commands

All that is needed now is to click the Refresh button in Source Control.

Source Control - synchronize repository

As you can see from the picture above, the current branch is develop and the third table we created in this branch appeared in remote changes.
By clicking Get Latest, our second developer will synchronize his dedicated database with the develop branch and will be able to get into gear.

Summary

The Source Control functionality allows you to commit changes to your current version control branch, fetch recent changes, and view change history right from Microsoft SQL Server Management Studio. This can help with speeding up and simplifying your development process. To switch the branch you’re committing to, you will need to use your version control system’s app or console commands. Download dbForge Source Control add-in and check this functionality for yourself with a free 30-day trial!

Leave a Comment