Link a SQL Server database to a Git repository in GitHub

GitHub is a web-based platform for software developers that enables the storage and management of code and the tracking and control of code changes via Git repositories. It makes it easy for developers to collaborate on projects and to create and manage issues, bugs, and feature requests. They can also automate testing and build processes and workflows during deployment through integration with various CI/CD tools.

Prerequisites

Make sure that you have:

  • Git for a Windows client installed on the machine.
  • GitHub account.

The article covers the following topics:

We’ll create a new repository on GitHub, so make sure you have a GitHub account.

Step 1: 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: Create 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 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 an existing repository, skip this step.

6. To keep the changes, click Create repository.

Step 2: 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.

Note

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

If you didn’t add any files to the repository when you created it for the first time, execute the following commands:

  • git commit --allow-empty -m initial

  • git push

Copy the URL of the repository on GitHub

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

2. On the Link page of the Link Database to Source Control wizard, click + in the Source control repository field.

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).

Note

The repository folder name represents a path to the local copy of the remote repository and must not contain the URL of the remote repository.

Link DB to Git

4. Optional: Click Test to verify that the database has successfully been connected to source control.

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 apply the changes and close the Source Control Repository Properties dialog.

6. Select a database development model: dedicated or shared.

7. Click Link to establish the connection between the database and source control.

Link DB to Git

The Refresh progress window opens automatically, showing the stages of the refresh operation. The linked database gets the following icon in Object Explorer.

Linked DB

Step 4: After linking to the GitHub repository

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 section, select the objects, and click Commit. For more details, see Commit changes.

If you linked a database that is already in source control, update your database to the latest version: go to the Remote changes section, select the objects, and click Get Latest, which is available only in the dedicated model but not in the shared model. For more details, see Get the latest version.

Want to Find out More?

Overview

Overview

Take a quick tour to learn all about the key benefits delivered by dbForge Source Control for SQL Server.
All Features

All features

Get acquainted with the rich features and capabilities of the Source Control in less than 5 minutes.
Request a demo

Request a demo

If you consider employing the Source Control for your business, request a demo to see it in action.
Ready to start using dbForge Source Control for SQL Server?