DEV Community

nineismine
nineismine

Posted on

How to use Visual Studio 2019 to access and check in code to GitHub in 15 minutes

Are you ready to learn how to check in code to GitHub in 15 minutes?


Over the last couple years in my organization we have been tasked with moving from TFS to Git. While Git is an awesome tool , it can be a little daunting if you have to pick it up and learn it on your own. There are plenty of great resources that explain WHAT git is.There are even plenty of articles that will go into detail about all of the things I am going to cover here! For that reason we are just going to focus on how to use it in, and specifically how to check in code to GitHub in 15 minutes!

One of the first things you will want to do is create a GitHub account if you do not already have one.

Adding your GitHub Account to Visual Studio.

One of the first things we need to know about when using Git is how to access it. While there is a stand alone Git client and it can come in handy for certain things. The built in client for accessing Git In Visual studio really simplifies the steps required to download and check in code.
To access Git related options in Visual Studio we need to look no farther than the "Team Explorer". Under the Team Explorer we have several options that we will be covering throughout this article.

In the menu select View > Team Explorer

Team Explorer

 

 

 

 

 

 

 

 

Here you will be presented with the "Connect" dialog box. If you have created a GitHub Account for yourself now would be a good time to connect it.

Team Explorer > Manage Connections (it looks like a green plug) > Connect to Github

Fill in your account data

That's it! You are connected and can now sync your local projects to GitHub! Speaking of which....

Now that I am connected how do I get started with this GitHub thing? I'm Glad that you asked!

 

 

How to create a local repository

When it comes to GIT and creating and updating code in a repository we first need to understand the concept of the local and remote repository. The Local repository is the collection of files in their current state on your computer, git keeps track of the changes that occur on your local system, and then you give it the correct command it updates the local repository to a new version on your local system. After updating the local system however you still need to push your changes to the remote repository to complete the process of updating your remote repository.
In order to get started with GIT this means that we need to either create a local repository or import one to our machine so that Git knows what files it is keeping track of. To do this with Visual Studio it's actually a pretty simple task.

  1. Open Team Explorer:
  2. Navigate to the lower pane of Team Explorer to the section labeled "Local Git Repositories"
  3. Click "new" and specify a location.
  4. Hit the "Create" button.
  5. This simply adds adds git tracking to your local repository.

 

How to import a remote repository.

Now that we know how to build a project and create a local repository , let's look at how to pull a remote repository so that we can modify it. For this example we will use Github as the example.

  1. Navigate to Github
  2. Find a repository that you want to clone. In this example we will use one of mine.
  3. Hit the "Clone or Download" button.
  4. Hit Open in Visual Studio.

 

 

 

 

 

 

 

or

  1. Copy the URL that is in the Clone with HTTPS dialog
  2. Go back to Visual Studio > Team Explorer > Connect > Clone >
  3. Paste the URL into the Open from GitHub
  4. Choose a Local Path
  5. Hit Clone

 

Remote... Local? What's the difference?

I said I would avoid getting too deep into HOW Git works but I want to make sure I touch on this topic really quickly because it is important.

I essence when you add a project to a Git Repo we now have 3 stages that we are tracking in terms of your stored files..

      1. Your active workspace. This is where your current files live any time you make a change and save it, you make real changes to your active workspace but they do not automatically apply to your Local repository. (I think that this is one of the places people get confused with GIT)
      2. Your Local Repository. You can think of the magic behind git as a change tracker.. your local repository is a record of the last changes you made to any file that is being tracked by GIT as of your last commit.
      3. Your Remote repository. After making changes to your local repository you have to then commit them up to the remote repository. These are the three states of files being tracked by git. Understanding this process will make it much easier to understand what you are doing with GIT and WHY.

Ok! Now that I have a repository... What the heck do I do with it?

So keeping in mind the last section , we need to make changes to our code (our active workspace) > Apply them to local and then apply them to remote.

How to modify your active workspace

This is the easiest one, just make a change and save it like any other file. When you make an uncommitted change to the active workspace you will see a red checkbox next to the uncommitted file in the Visual Studio Solution Explorer.

 

How to commit your changes to the Local Repository.

Open Team Explorer > Click Changes > Enter a commit message to the Commit Dialog > Hit the "Commit All" button.

After the commit you get a message that shows you that you have made a Local Commit.

Now Sync by clicking the blue text that says Sync in the message. (you can see it in the screenshot above)

If we keep with the above explanation of the stages of git we are now in stage 2 , we have committed our changes locally.

On the Synchronization tab, under outgoing, we have an uncommitted change that is waiting to be pushed to remote.

Click push and it will push your changes to the master branch. That's it! you have learned how to check in code to GitHub in 15 minutes!

 

Top comments (0)