To get started to using Git with either GitHub or Azure DevOps, we are going to go over the options available to you to connect and clone repositories in either service using Visual Studio Code. Additional articles about using Git in VS Code will be coming in the near future.
NOTE: This article was tested and written for a Windows Host running Windows 10.
In this installment, we'll be going over the following.
What is Git
Why Git is important for IaC
Installing Git using Chocolatey
Clone a Public GitHub Repo in VS Code using the Terminal
Clone a Public GitHub Repo using VS Code in the Control Pallet
Clone a Private GitHub Repo in VS Code
Clone a Private Azure DevOps Repo in VS Code
Conclusion
SPONSOR: Need to stop and start your development VMs on a schedule? The Azure Resource Scheduler let's you schedule up to 10 Azure VMs for FREE! Learn more HERE
Git is a versioning control system that allows you to track changes to anything you are working on (Code, Documentation, files, images, etc...) under a single primary directory called a repository. However, a much more colorful description is given by its creator Linus Torvalds (also the creator of Linux) at the top of the README file in the initial revision of Git.
GIT - the stupid content tracker
"git" can mean anything, depending on your mood.
- random three-letter combination that is pronounceable, and not
actually used by any common UNIX command. The fact that it is a
mispronounciation of "get" may or may not be relevant.
- stupid. contemptible and despicable. simple. Take your pick from the
dictionary of slang.
- "global information tracker": you're in a good mood, and it actually
works for you. Angels sing, and a light suddenly fills the room.
- "goddamn idiotic truckload of sh*t": when it breaks
This is a stupid (but extremely fast) directory content manager. It
doesn't do a whole lot, but what it _does_ do is track directory
contents efficiently.
That last sentence describing how it tracks directory contents extremely fast and efficiently is the crux of why Git is so popular.
In the IT Industry it is common to utilize scripts, written in a variety of scripting languages, to automate redundant tasks that must be performed on a regular basis. Sometimes these scripts are documented as to how they work and what their purpose is, many times they are not. As a collection of scripts grows over time,variations of the same script may be written without any meaningful way of distinguishing them other than by asking the original author. This also means that tracking changes made to these scripts is a manual process that is left up to the whims of their maintainers.
If you were to use this same methodology while adopting Infrastructure as Code you would be setting yourself up for failure. This is why using a version controlling system such as Git is so important.
With Git, any changes made to a repository must be first be done to the local copy on your machine, committed as a change that can include comments as to the change, and then pushed to the master version of the repository wherever it is hosted (GitHub, AzureDevOps, Bitbucket, etc). This process ensures that any changes made to the repository are trackable and that you can determine when and who made them.
Open up an Elevated PowerShell prompt and run the following command to install Git using Chocolatey
choco install git -y
Open up Visual Studio Code and click on Terminal and then New Terminal.
Next, clone the 100DaysofIac Repo using HTTPS, by running the following command in the terminal.
git clone https://github.com/starkfell/100DaysOfIaC.git
You should get back the following response as shown below.
You'll notice that the 100DaysOfIaC Repository has been downloaded into a directory in your User Directory.
In VS Code, press CTRL + Shift + P to open up the control pallet
Type in Git until you see Git:Clone and click on it.
Copy and paste in the 100DaysofIaC URL into the prompt and press Enter.
You should see the contents of your User Folder appear, click on the Select Repository Location button.
NOTE: If you see the 100DaysOfIaC Folder from the previous example, delete it first.
In the bottom right hand corner of VS Code, you will be prompted to open the cloned directory. Click Open.
In the upper left hand corner of VS Code, you should see the contents of the 100DaysOfIaC repository in the Explorer window.
You need access to a GitHub Account and an existing Private repository in GitHub to complete the next steps. Setting up a new GitHub account and creating your own private repo takes only a few minutes, you can start here.
In VS Code, press CTRL + Shift + P to open up the control pallet
Type in Git until you see Git:Clone and click on it.
Copy and paste in the URL of the Private Repo into the prompt and press Enter.
You should see the contents of your User Folder appear, click on the Select Repository Location button.
Next, you will be prompted to login to GitHub.
If you are setup with two-factor authentication, you will be prompted again.
In the bottom right hand corner of VS Code, you will be prompted to open the cloned directory. Click Open.
In the upper left hand corner of VS Code, you should see the contents of the private repository in the Explorer window.
You need to have access to an existing Microsoft account and a repository already setup before you can complete the steps below. You can sign-up for an account here.
In VS Code, press CTRL + Shift + P to open up the control pallet
Type in Git until you see Git:Clone and click on it.
Copy and paste in the URL of the Private Repo into the prompt and press Enter.
You should see the contents of your User Folder appear, click on the Select Repository Location button.
Next, you will be prompted to login using your Microsoft Account.
NOTE: If you are setup with two-factor authentication, you will be prompted again.
In the bottom right hand corner of VS Code, you will be prompted to open the cloned directory. Click Open.
In the upper left hand corner of VS Code, you should see the contents of the private repository in the Explorer window.
In the next few installments we'll be covering additional information on working with Git and how you can get manage and deploy ARM templates from within VS Code. In the meantime, we highly recommend reading the first three chapters of the Pro Git Book by Scott Chacon and Ben Straub.