In my last post I explained how you can use GitHub as a platform for Azure Automation runbook versioning and source control. Another tool IT Pros (like myself) use more and more is Visual Studio. This article walks you through the process of enabling GitHub as a source control platform for you Visual Studio projects.
Before we get started: Git vs. GitHub, what’s the difference?
I will keep this very short. In case you need more information, the internet is full of it. So I will keep this part very short, but it’s important to understand the content of this blog post. Git is a source control management system that works with repositories and branches. These concepts can be used create copies/snapshot of your code and allow you to go back in time to access older versions of your code. GitHub on the other hand is a web-based platform to manage Git repositories and branches. Your code can be shared with others and they can even contribute to your code. It’s important to understand this difference for the walkthrough in this blog post.
First of all I will create a new repository in GitHub. That’s it from the GitHub side, job done.
Install Git for Windows
On my dev machine I need to install “Git for Windows”. It can be downloaded from the appropriate website. The installation is straightforward.
Configure Visual Studio for Git Source Control
Now I start Visual Studio on my dev machine and create a new project. In this example I create a project for a “Azure Resource Manager” template, gut it could be anything you like.
I check “Add to source control” which gives me two option for source control: TFS or Git. There is no “GitHub” option available. This means that I need to install “Git for Windows” on my dev machine and create a local Git repository. Everything will be source controlled locally on my machine in a first step.
If you miss to activate this checkbox, you can add your solution to source control later. One important thing to know: In the background, a folder with the name “.git” is created on the Visual Studio solution folder. This is used for the Git repository management. Will dive into this later.
I will now commit the complete Visual Studio project with all containing files to Git.
If this is your first use of Git, you possibly are forced to enter some personal details. This information is used in the source control system when your work will be commited.
Now I can commit my work to the Git repository’s master branch (which is the only branch that exists at this time).
Done, my work is now commited to the local Git source control system. As you can see in the next screenshot, I could now sync the work to a remote repository which can be GitHub. With that, I make my work accessible to everyone that can access my GitHub repository. But if I would click “Sync” now, the process would fail.
Why? Because by hitting sync, my local Git repository branch “master” would be synched to my GitHub repository and in there to a branch with the name “master”. However, my GitHub repository already has a branch named “master”. That said, I first need to create a new local Git branch with a different name.
Prepare Git to work with GitHub
In Visual Studio I can use the “Team Explorer – Branches” to create a new branch from my “master” branch.
Now I change back to “Team Explorer – Changes” and click on the “Sync” link.
Because this is my first sync to a remote repository, I need to add the exact path. In my case, the remote repository lives on GitHub, so I need to provide the correct URL to that repository.
If you are unsure what URL to use, just use a browser to connect to your GitHub repository and copy the complete link.
Once you entered the URL the sync should work.
If you connect back to the GitHub website, you should now see that a new branch has been created. It has the same name as the local Git branch. And you should see the appropriate Visual Studio project files.
When using “Git CMD” that was installed with “Git for Windows”, you can change to the Visual Studio project folder and see that a remote repository was configured for your local Git repository.
Ongoing changes in your Visual Studio project
If you make changes now, you can commit those to the local Git source control system and in a second step push then directly to the remote repository on GitHub.
The initial setup might be a bit tricky, but once you have this up and running, it works like a charm! GitHub repositories can be used free of charge (depending on your exact needs there might be costs) and are a great way to collaborate with your peers and people from all over the world.