Link a database to Git

dbForge Source Control is a Microsoft SQL Server Management Studio (SSMS) add-in that is designed to manage database changes in most well-known version control systems. With the help of the tool, you can version-control database schemas and data, roll back unwanted changes, handle static data conflicts, and maintain the integrity of the database. Via Source Control, you can link your database to Git, which is a distributed version control system. Git is aimed at coordinating the work of multiple developers and provides non-linear workflow.

Note

dbForge Source Control 2.2 and later supports Git for Windows client. Thanks to it you can work with all possible connection and authentication types, as this task is delegated to the Git client. Before trying to connect your database to Git, make sure that you have Git for Windows client installed on your machine.

Prerequisites:

  • Git for Windows client installed on the machine.
  • Git repository created locally or cloned from the remote repository.

The article covers the following topics:

We are going to create a new repository on GitHub, so make sure you have a GitHub account.

Create a new repository on GitHub

1. Sign in to your GitHub account.

2. In the upper-right corner of the page, click the + dropdown menu and select New Repository.

Create a new repository on GitHub

3. On the Create a new repository page that opens, enter the name and optional description.

4. Select the visibility mode:

  • Public to make the repository to be visible for everyone on the Internet.
  • Private to restrict access to specific users.

5. Make an initial commit to the repository by selecting the options:

  • Add a README file: Creates a README file containing a description of the project.
  • Add .gitignore: Select files to be ignored. The .gitignore file will contain a set of ignore rules.
  • Choose a license: Grant permissions so that users can use, change, and distribute the product.

The default name of the branch is main. If you want to update the name, click settings under the options.

Initialize the repository on GitHub

Note

If you import the existing repository, skip this step.

6. To keep the changes, click Create repository.

Work with a local Git repository

dbForge Source Control does not allow working with a local repository. The repository needs to have both an adjusted remote branch and a local branch tracking the remote one.

To work with a local Git repository, perform the following steps:

1. Create an empty folder on the disk of your computer where the local repository will be stored.

2. Start Windows Command Prompt (CMD).

Note

To change the disk drive and navigate to the empty folder you created, in the Command Prompt, use the cd command.

3. Type git init --bare to create the main repository locally.

4. Navigate to a different folder and clone the local repository using git clone <pathToMainRepo>.

Note

<pathToMainRepo> is the URL to the remote repository to which you will commit the changes and from which you will pull the changes to the local copy of the repository.

5. Execute these CMD commands in the folder in question:

  • git commit --allow-empty -m initial to make an initial commit.
  • git push to push the changes to the remote repository.

Work with a local Git repository

6. Link dbForge Source Control to that folder in the following order:

  • In Object Explorer, right-click the database you want to link to source control and select Source Control > Link Database to Source Control.

  • In the Link Database to Source Control dialog that opens, click + in the Source control repository field to open the Source Control Repository Properties dialog.

  • In the dialog that opens, do the following:

    • Select Git from the Source Control system dropdown list.
    • In the Repository folder field, click More options icon More to select the repository folder.
    • Optional: In the Repository name field, you can change the name of the repository folder.

Select the repository folder

  • Click Test to verify that the database has successfully been connected to source control.

  • Click OK to close the Source Control Repository Properties dialog.

  • In the Link Database to Source Control dialog, select a database development model and click Link.

Clone the repository using the command line

1. Create an empty folder on the disk of your computer where the local repository will be stored.

2. In the GitHub account, switch to the Repositories page and select the repository you want to copy to your computer.

3. On the <> Code tab, click the Code dropdown menu, select HTTPS, and click Copy to clipboard button Copy to Clipboard to copy the URL of the repository.

Copy the URL of the repository on GitHub

4. In the Command Prompt, type the git clone command, paste the URL directory you’ve copied, and press Enter to create a cloned copy of the repository.

$ git clone https://github.com/Jordan-Sanders/new-repo .

Note

To clone the repository to the folder you have created, make sure to place a dot at the end of the command. Otherwise, another subfolder will be created.

If you have not initialized the repository while creating it in the Git account, perform the following steps after cloning the repository:

5. Execute the git commit --allow-empty -m initial command.

6. Execute the git push command.

1. In Object Explorer, right-click a database you want to link to source control and select Source Control > Link database to Source Control.

Link DB to Source Control

The Link Database To Source Control wizard opens.

2. On the Link tab of the wizard, click + in the Source control repository field.

Link DB Wizard

3. In the Source Control Repository Properties dialog that opens, select Git from the Source Control system dropdown list and provide a path to your local Git repository where dbForge Source Control will store SQL scripts.

Note

To work with a remote repository, you need a clone of this repository. To retrieve a local clone, execute the git clone <url> <path> command or use the GUI tool. After the successful creation of the clone, specify the path to the folder containing the clone in the Repository Folder field of the Repository Properties window. You can specify a path to a subfolder in the repository (not .git).

Link DB to Git

4. Click Test to check the connection.

Note

Keep in mind that you should work with the current repository branch. For this, it should have at least one commit.

5. Click OK to close the Source Control Repository Properties dialog.

6. Select a database development model. For more information, refer to the Dedicated model and Shared model topics.

7. Click Link. If a database is linked to source control successfully, a database icon in Object Explorer will be changed to show that a database is linked.

Linked DB

In this video, we will explain how to collaborate on the same database project using Devart’s Source Control for SQL Server. Video provides the case where two developers try to push database changes to a Git remote repository simultaneously.

How to collaborate on a database project with dbForge Source Control add-in

After linking to Git

If you are the first person to link this database, add the database objects to source control in an ‘initial commit’: go to the Local changes group, select the objects and click Commit. For more details, see Committing changes.

If you linked a database that is already in source control, update your database to the latest version: go to the Remote changes group, select the objects, and click Get Latest that is available only in the Dedicated Model mode, but not in the Shared Model mode. For more details, see Getting latest version.

To learn more about versioning SQL databases linked to GIT, you can refer to the following pages: