Git Primer for SQL Server Pros

Do You Even Source Control?

One of the greatest assets we have as data professionals is our unrelenting desire to understand how things work and leave them better than the way we found them. We often write scripts, code, and even applications to help us do less work solve ALL the problems. And “solving problems” is our middle name.

When we solve these problems though, what do we do with the code?

Sure, we’ve all heard of source control but are you leveraging it? Do you have a development pipeline for SQL Server schema changes? Do you have a central repository for your useful scripts?

If you haven’t worked with Git before, this will be a great place to start.


Setup

Download and Install Git

In addition to downloading and installing Git from Git’s website, the Git source code is also contained in a Git repository and maintained on GitHub. In summary, you can get Git’s Git repository by using Git. Quite the paradox.

git clone https://github.com/git/git

I included the above strictly for entertainment. Just download the thing like a normal person now.


Git Bash (Command Line)

Git Bash is the command line utility that is installed in the previous step. You can open the bash shell from the start menu or from the right-click context menu (a checkbox during setup).

For the purposes of this guide, I will be using the bash shell.


Global Configuration (Name and Text Editor)

The first post-installation task to take is to make sure you get credit for your work (it’s a requirement). Do this by adding your name and email address to the global configuration. The –global option will ensure that the configuration settings are applied to any project that you work on. Without the option, you would need to specify the configuration on a per-project basis.

# Set the name and email properties in the global configuration.
git config --global user.name " Your Name "
git config --global user.email " Your Email Address "

Next, configure your preferred text editor. Since I have no idea how to use vim (the built-in editor), I always set this configuration option. Any text editor will do. Enter the path to the .exe file and you are good to go. The text editor will open when you encounter merge conflicts or if you are working on keeping your commit history clean and organized. I will cover this concept in a later post.

git config --global core.editor notepad.exe

More Info -> Getting Started


Build Your Own Git Repository

Why wait until tomorrow when you can share your code today. The following guide will show you how to quickly build a Git repository and sync it to GitHub.


Create a Public Repository in GitHub

You should already have an account, right? It’s 2018. After you quickly create your account, let’s build our first open source repository.

Note: Unless you have a subscription on GitHub, you are only able to host unlimited public repositories. So please do not post your sensitive files on an open source project.

After logging in to GitHub, on the top right corner of the screen, click on the ‘+‘ sign and select New repository.

  1. Enter a name for your repository
  2. Choose the ‘Public’ option
  3. Check the box to initialize this repository with a README. A README is the text file users will see when they navigate to your repo on GitHub. Fill it with details of your project.
  4. Add a license. Click the info button to see which is suitable for your project.

Next, copy the URL shown in the image below.


Clone the Repo to Your Local Workstation

Open up Git Bash and navigate to a directory where you would like your repository to reside. Replace the URL with your own, and run the code below to pull down the repository we just created. My repo is being created in C:\Temp and in a subdirectory called git-my-blog.

git clone https://github.com/clagreca03/git-my-blog.git git-my-blog

This is what you should see after running the command.

In your file explorer, you will see your repo.

Inside the repo, you will find your license and a hidden .git directory (unless you aren’t showing hidden files). If you ever want to remove your local copy from source control, simply delete the .git directory.

Change directories into your repo on the command line and run

git status

to see that your branch is up to date with the remote repository.

Running

git remote

and

git remote -v

will show you a list of your remote repositories (the default is named origin) as well as the locations of these remote repositories.


Add and Commit Files to the Repo

Copy / Paste or Move your files into your repository.

Running

git status

will now show you files that have changed.

Stage the files to be committed with

git add .

You can also add files individually if you choose to break up your commits into smaller manageable parts. This is a best practice for larger projects. The ‘.’ means we are adding everything that has changed.

Commit the changes to your local repo.

git commit -m 'Create New Test Table'

Running

git status

now will show you that your local branch has 1 new change that does not exist in your remote repository on GitHub.

Push the changes to GitHub with

git push origin master

A dialog will pop up to authenticate and after doing so, your credentials will be cached for follow-up commits. SSH is an option as well and if you would like to set that up instead, see Connecting to GitHub with SSH.

After providing your credentials, your local changes will be synced to GitHub.


Meanwhile, on GitHub

This is what you will see in your remote repo.

Clicking on commits will list your repository changes.

And finally, clicking on the commit itself will show you the diff of all the files that have changed.


Next Steps

Now that you are finally sharing your work with the world, you are probably wondering what to do next. Feel free to modify your files or create new files and sync your local changes to GitHub. The following simple workflow will make that happen:

  1. git add <filename> OR git add .
  2. git commit -m ‘ Your commit message ‘
  3. git push origin master

I will have guides for working on multiple computers and working with multiple developers in the near future. Subscribe to this blog if you would like to receive an email when they are ready.

If you are in the zone and can’t wait to continue learning, explore the Git Documentation.

Leave a Reply

Close Menu