SSMS 22 Git Integration

Problem

In SSMS 22, there is Git integration to manage SQL code versions and repositories. How do you use this functionality?  Can you walk through a step-by-step example?

Solution

In this article, we will explain what Git is and how to use it in SSMS 22.

What is Git?

Git is an option in SSMS to integrate your code with Git (a free version control system) and GitHub (an online platform to store and share your code). Optionally, you can connect to Azure DevOps, a Microsoft centralized platform that hosts Git repositories and manages the software lifecycle.

Requirements

  1. First, we need to have SSMS installed.
  2. Second, we need a GitHub account.

Getting Started

Open SSMS 22 and go to Git > Create Git Repository.

create git repository from SSMS

SSMS will detect your active Git repository. It will create a default repository. If you don’t have Git installed, refer to this link.

connect ssms and git

You may also want to have a GitHub account as well. Go to the prerequisites section to create your GitHub Account and login to SSMS with that account.

connect ssms and git

Once created, you will have a repository in GitHub.

git repository

Also, you will have a local repository on your machine using Git.

local git repository

Working with Git

You can view your solution using the Solution Explorer in SSMS. To open it, go to View > Solution Explorer in the SSMS menu.

ssms and git options

Right-click the Folder view and select the Open option.

git folder

Finally, you will be able to see the files of the project.

Adding files to the project

In the Solution Explorer in SSMS, right-click the project and select Add New file.

add files to git project

Name it file.sql or any name of your preference.

sql files

How to add existing files to Git and GitHub

To add existing files, right-click the solution and select Open in File Explorer.

add files to git and github

Copy existing files to the folder. In this example, we copied a file named uspGetWhereUsedProduct.sql.

sql files locallly

How to commit changes

Commit will save the data in the local repository. To commit the files, in the SSMS menu, go to Git > Commit or Stash.

commit changes to git

You will notice the letter A in the files added as shown below. A means added.

Add a description for the changes and press Commit All.

git changes

Note that after the commit, a lock will be displayed.

git file lock

View changes

With Git, you can register different versions of your code on different days. To view the local history, we will go to the Manage Branches option. In SSMS, go to Git > Manage Branches.

git braches
git braches

Revert version

If you need to revert to a previous version, you can use the revert option. Just right-click the version that you want to revert and select the revert option.

revert sql change using git

Save modifications

Now, we will modify the file and see the registered changes. Double-click a file in Solution Explorer.

save changes to git

Add some code.

modify file

Commit the changes.

Notice that the letter M for modified is displayed. Add a description to the change and press Commit All.

git changes

Sync (Pull then Push)

The Sync option will let you upload your local changes to the remote repository (GitHub or Azure DevOps). Pull downloads changes in the remote repository (GitHub) and push uploads of local commits to the remote repository.

ssms menu for git

If you go to GitHub, you will be able to see the files there.

github files

Create new branches

Finally, we will learn how to create new branches in Git.

The branch is a copy of the project that you can modify without affecting the original branch. For example, you are working on a new feature, and you are doing some tests first. Here we will show how to create a new branch.

In this example, we will create a branch from the master. In the Git Menu, select New branch.

create a git new branch from ssms

Write a name for the branch and select the branch it is based on, and press Create.

create a git new branch

In the SSMS, go to Git > Git Changes.

ssms menu

You can select the new branch create.

ssms and git changes

If you change something, you can also commit your changes.

ssms and git changes

Next Steps

For more information about Git, refer to these links:

Leave a Reply

Your email address will not be published. Required fields are marked *