Introduction
Previously, we created a tutorial to connect Visual Studio Code with GitHub. Now, we will show another useful tool to track changes in our SQL code. The tool is GitHub Desktop.
What is GitHub Desktop?
It is a free tool that works with Git and GitHub. This tool easily synchronizes Git and GitHub.
For example, if you have code in SSMS, Notepad ++, or other tools, you can easily synchronize your code with Git and GitHub using GitHub desktop and have a control of the version and revert your code to previous versions. You can use the GitHub web to upload your code when it is ready. However, the GitHub version will help you to save the code locally until it is ready to upload to the GitHub website.
In this tutorial, we will teach the following topics:
- Create a new repository.
- Add and edit local files in GitHub Desktop files.
- Edit local files in GitHub Desktop files.
- Publish your local repository data to GitHub.
- Pull files from GitHub to Git using GitHub Desktop.
- Edit data in GitHub and pull the changes in Git using GitHub desktop.
- Create a new branch in GitHub Desktop.
- Merge the branch into the main branch.
- Amend commit.
- Reset to commit.
- Checkout commit.
- Revert changes in commit.
Requirements
I am assuming that you already have a GitHub account. If not please go to our article Git, GitHub, and Visual Studio Code for SQL Server. The article will guide you in registering there. In addition, please install GitHub Desktop using this link. There is a version for Windows and another for Mac. In the long run, a Linux version may be available.
Once installed, you will need to authorize GitHub Desktop to connect to GitHub. Press continue and provide your credentials to connect.
Also, authorize the GitHub desktop to connect.
Create a New Repository
The first thing we will do is create a local repository to store our SQL scripts. In the menu, select File>New repository.
Provide a name for the repository, optionally a description. You can generate a readme file for instructions.
The Git ignore is used to ignore some files when you commit. You can select these files to ignore them.
Also, you have different license types like GNU, Apache, or BSD.
Once you are ready, press the Create Repository button.
Once created, you can publish your repository to GitHub on the web, open your files in VSCode, or show the files in Windows Explorer.
Add local files in GitHub Desktop files
Now that we have a repository, we will press the Show in Explorer button.
We will add two files from here:
- https://github.com/dcalbimonte/sqlservercentral/blob/main/HumanResources/Tables/Employee.sql
- https://github.com/dcalbimonte/sqlservercentral/blob/main/HumanResources/Tables/Department.sql
Download the files.
Copy the files downloaded into your local repository.
If you go to the GitHub desktop, you will notice that 2 files were added. Enter a comment and press the Commit to main button to save the changes.
Edit local files in GitHub Desktop files
You can edit your files in your local repository using any tool of your preference. In this example, I will open the department.sql file using the SSMS.
Also, add some comments to the SQL file like the author and data, and save the file.
If you go to the GitHub Desktop, it will show you the changes in green and tell you that the file was changed.
Add a description of the changes and press the Commit to main button.
Publish your local repository data to GitHub
If we think we are ready to publish our data, we need to press the Publish repository button.
First, add a Name and a description. Then press the Publish repository button.
Note that the new repository is published on the web.
If you go to the website, you will see the new repository published.
Pull files from GitHub to Git using GitHub Desktop
When we pull data from the GitHub web to our local Git repository, we send data from the GitHub website to our local Git. To do that we will follow these steps:
First, download the Address.sql file from this URL: https://github.com/dcalbimonte/sqlservercentral/blob/main/Person/Tables/Address.sql
Secondly, add the Address.sql file to your repository on GitHub website you created (check the Publish your local repository data to GitHub section if you didn’t do it). Use the Add file button and select the Upload files to upload the file.
Once added, Commit the changes.
Finally, in GitHub Desktop, Pull the change. This will update the changes in our local file.
Edit data in GitHub and pull the changes in Git using GitHub desktop
In this section, we will modify one SQL file in our repository on GitHub and then pull the change to our local repository.
First, click the Address.sql file.
Secondly, select the switch to the editor to edit the file.
Thirdly, add comments with the author and the creation date and press the Commit changes button.
Also, add a name for the changes, and a description and press the Commit changes button.
When you go to GitHub Desktop, you will notice you have Pull notifications. You can press the Pull origin button to update your changes.
Alternatively, you can go to the Menu and select Repository>Pull.
If you open your local file in your local repository, you will notice the change.
Create a New Branch in GitHub Desktop
Branches help you to test the code and experiment features. By default, you create everything in the main branch. For example, you want to add a new column to your code but do not want to affect the main code until it is fully tested. For that, you can create a new branch. In this article, we will teach you how to work with branches.
To do that, we will use the following steps. First, select Branch>New branch in the menu.
Alternatively, you can press the Current Branch arrow and press the New branch button to create a new branch.
Also, you need to specify a name for the branch and press the Create branch button.
You will see no local changes at the beginning and you will have to option to Publish the branch in GitHub.
Open the department.sql in SSMS, select the sentences after the first GO, and delete them.
Save the changes and go back to the GitHub desktop. You will note the code deleted in blue. Commit the changes.
In addition, return to the main branch.
Also, go to the menu and select Branch>Merge into current branch. This option will save the changes of the branches into the main branch.
Select the branches that you want to merge and press the Create a merge commit button. Now you will have your changes merged.
Clone repository to Git
The Clone option will download a repository from GitHub on the web to a local repository in our local machine. In this example, we will use our existing GitHub Repository created in our article Git, GitHub, and Visual Studio Code for SQL Server, we created a repository on the GitHub website. We will clone that repository locally. To do that, we will follow these steps.
First, go to the menu and select File>Clone repository.
Go to the URL tab and select the URL of your GitHub repository. Choose a local path and press the Clone button.
Once created, you will have a copy of the repository in your own local computer.
Amend to commit
In the History tab, you have options to work with the commit. The Amend commit is the first option for commit. This option allows editing the selected commit.
To use it follow these steps. First, right-click the commit option and select Amend commit.
Change the name and description and press Amend last commit button to save the changes.
You can modify the comments or the description of the branches to compare.
Reset to commit
Resetting is used to undo changes made in a commit.
Reorder commit
Also, we have the reorder commit option to change the order of the commits. You can use the Up or Down arrows to move your comments and change the order of the commits.
Just select your commit and press the up or down keyword to change the order of the commits. This will change the order of the commits.
Revert changes in Commit
Finally, we have the revert changes in commit option to revert changes in GitHub.
The revert changes in Commit will create a new commit with the state before the selected commit.
Conclusion
In this article, we show how to use GitHub, how to handle changes, how to commit changes, how to work with branches, and the different options to commit the file.