Last updated on 2022-07-07
Here’s a video on how to connect Visual Studio Community Edition 2022 to your GitHub account, so you can save a project to GitHub, push your code changes to GitHub, share your GitHub project with another GitHub user, and download someone else’s project from GitHub.
In this video, I’ll demonstrate connecting Visual Studio Community Edition 2022 to GitHub.
I’m only going to cover a couple of the very basic things you can do with GitHub. I’m not going to cover some of the things that you would get into if you were working on a team or using GitHub for a professional project.
This will mainly let you work with your personal projects where you’re the only developer who’s working on them.
To start out, I will assume that you’ve gone to GitHub.com and created an account there.
I’ve created one here called CodingWithScott. Once you create your account, your URL will be github.com/WhateverYourUsernameIs and that’s where all your repositories will be. Your repositories are just copies of your source code.
Create a project in Visual Studio
The first thing we’ll do is create a new project in Visual Studio and then push that up to our GitHub repository.
If we go into GitHub and look at my repositories, we see that I have two of them here. These are ones that I’ve already created.
Once we create the new project, we’ll see our third project up here.
I’ll start by going into Visual Studio and click “Create a new project”. I’m going to pick a console app for this video. It really doesn’t matter. This is just for the demonstration.
And I will call this MyGitHubTest project. I’ll change the location to my E drive and my \Projects folder just because that’s how I like to keep my projects in one place on my local computer.
Click “Next” and select .NET 6. Again, the version doesn’t matter. This will work for any type of project.
And now I’ve created the project.
In the upper right section of Visual Studio we have the “Solution Explorer” area, and at the bottom of that, we have a “Git changes” tab.
If we click on that, we’ll see where we’ll work with Git in the future.
Right now, we haven’t connected to anything, so we just have these two buttons.
Push your solution’s source code up to GitHub
Now we’re going to push this project to GitHub.
The way I like to do this is down at the bottom right of Visual Studio.
We have this little text that says, “Add to Source Control”, so I left click on that and it shows me Git as the source control tool. I’ll click on that.
Now it shows me “Create a Git Repository” here in the middle screen.
This is where I add the information about the repository, the copy of the source code I’m going to save on GitHub.
I have my local path to the project.
The second thing here is a .gitignore template which is set to “default”.
Since I’m working with Visual Studio, I’ll click this, scroll down, and select “Visual Studio”.
This holds a list of the files we have on our local computer that we don’t need to push to GitHub. This would be things like my user configuration.
Also, once I build this solution in fact, let’s do that I’m going to cancel out of here and do a quick build.
If we look in our directory now, we see I’ve got a “bin” folder and an “obj” folder and these are kind of the compiled versions of our program. We don’t need these in GitHub. We really just need the source code stored in GitHub – in this case, the CS project file and the CS file.
If we go back into here by selecting the Visual Studio .gitignore template, we tell GitHub when we push our code to GitHub, don’t push the “bin” folder, the “obj” folder, or user settings – things like that.
Next, we need to go to the account and on the first time you do this, you’re going to need to “Sign in”
So, I’ll click the sign in and I’m going to add an account for GitHub. It pops up into a browser page. It should authorize you here if you’re already logged into GitHub. If you’re not, you may need to log in and give permission to Visual Studio.
I’ll close that tab, and we see now the owner is my CodingWithScott GitHub account.
It has the repository name, which is usually the solution name, so we’ll just leave that.
You can enter description here if you want.
Then, an important thing here is the checkbox for private repository. If this is checked, once you push your code to GitHub, you’ll be the only person who can see the source code. If you uncheck this, that makes it a public repository, so that means anybody who goes to GitHub can see your code.
You might want to start out with just doing private repositories.
One potential problem you may run into the first time you start using source control is you may have passwords or keys or something that you need to keep private.
And if you uncheck this box and make your repository public, then anybody can see your passwords and keys. So, until you learn how to manage those, the best, safest thing is to just make this a private repository.
And then we see down here it says, “Push your code to GitHub”, and it gives you the URL for where this repository will be located on GitHub.
I’ll click create and push.
And if you notice, in the upper right of Visual Studio, now that we’ve logged in, we have a different screen for our git changes. We have a message here that says “Configure your username and email address before committing changes”, and then we have a message that the Git repository was created.
I’ll click on the “Configure your username and email address” link. This is just a Visual Studio thing. I just go with the default here. And set this in my global gitconfig. So now, this will be the username and email address Visual Studio uses for anything it pushes to GitHub, at least on this computer.
If we go back to GitHub, go to my CodingWithScott page and refresh, we see I now have three repositories with the My GitHub test project added.
I can click on that repository and see my solution file. It also has my .gitignore file. Here’s a folder for the actual project, and if I click on that, we see here’s my CSPROJ file and my Program.cs file. We can even click on the file and see the source code inside the Program.cs file.
Now let’s say I want to make a change to my project, so I’ll go into Visual Studio and make a very simple change. I’ll change the Console.Writeline(“Hello world”) to “Hello Scott”, and I’ll save that.
If you notice over here in the “Git Changes” we now see under this changes line Program.cs has changed. I can right click on this and say, “Compare with unmodified” and I’ll see over on the left in Visual Studio is the original code, the unmodified code, and over here on the right is the modified code with the change I made highlighted.
If I want to push this to GitHub, I will probably enter a message here. In fact, it’s required, so I’m going to say, “changed name”. Try to make your GitHub commit messages meaningful. It’s better than just typing random letters. If you ever go back and look at your history, then you’ll know what you did in that particular set of changes.
Now we have this “Commit all” button. I’m going to click the drop down to the right of it and I’m going to say “commit all and push”. In the future, I’ll create a more in-depth GitHub and Git tutorial video and maybe even a series.
Right now, the important thing is, if you commit your changes, you’re just doing that locally on compute on your computer. If you “Commit and push”, then you’re saying take my changes and push them up to GitHub. So, we’ll do that “Commit and push”.
And I get a message here that the changes were successfully pushed to “origin\master” and I get an option to create a pull request. That’s one of the more advanced things that you might do on a team. Right now, we’re not going to do pull requests, so I’ll just close that message.
But now if I go to GitHub and refresh, we notice that the source code for Program.cs says “Hello Scott” now, so it has our change.
Sharing your private GitHub repositories
As I mentioned at the start, I’m not going to get too in depth into everything you can do on GitHub. But I know a lot of times people are going through some of my guides, have questions, and they want me to look at their source code. So, I’m going to show you how to share your code with me or with anyone on GitHub.
I’ll go back up here to my project repository in GitHub and click on the “Settings” gear icon.
There’s an option on the left called “Collaborators”. I’ll click that and I need to re-enter my password. GitHub wants to be sure I’m the person who has permissions to give other people permissions. We’ll confirm that password, and now I can manage the access.
I’m going to add a person here who will have access to my private repository. I can search by someone by their GitHub username, their full name, or their email address. In this case I’m going to invite my main account and add “ScottLilly” as a collaborator to this repository.
So now my other main account in GitHub is going to be able to access this project even though it’s a private project. Now it’s private to everyone in the world, except for the CodingWwithScott user and the ScottLilly user. This will send out an invitation and then I have to go into my email or go into my other account and accept the invitation before I fully have permissions.
This is how you would share one of your private repositories with someone else on GitHub.
How to pull down code from a public GitHub repository
Next, I’ll demonstrate how to pull down someone else is public repository.
We’ll go back to my main page here, we’ll close Visual Studio, and I’ll go into my projects folder on my E drive.
If you notice, I just have the one MyGitHubTestProject – the one that we just created.
But let’s say I wanted to get a project from one of my ScottLilly GitHub repositories.
I’ll check my repository’s out here, and let’s say I want to get this Wordo, program. I’ll click on the repository. If you click on this green “Code” button, you’ll see here’s the URL you need to clone the repository. This is the location of the repository. You can click to the right of the “Code” button to copy the URL to the clipboard.
Now when I open Visual Studio, one of the options is “Clone Repository”. I’ll click on that this time and paste in that URL we just got.
Select the local disk path where I want to put this source code. I’ll put this in my E:\Projects folder. Visual Studio will create that folder and it will pull down the source code for the project.
Then I click the “Clone” button. This is a small project, so it clones it pretty quickly.
If we go look at our directories, we see I now have a Wordo folder with the solution. And the different projects and the different classes that were all pulled down from my ScottLilly\Wordo repository.
If I look in the solution explorer. Here are the files. For some reason Visual Studio displays the files in this format. I like to view it in the solution format, so I double click on solution, and now it shows it in kind of the standard way I’m used to seeing it.
Now I have all the source code from this public project out here on GitHub. And now I can build it locally and change it.
You could potentially make changes to it and attempt to push it to my repository, but then we get into some of the more advanced features of GitHub. For right now you would just use this.
If you see someone’s code on GitHub that you want to pull down and build locally and test with and maybe modify locally.
And that’s it for this video. Now you should be able to connect your Visual Studio Community Edition 2022 to your GitHub account.
You should be able to push changes from your project into a repository on GitHub and you should be able to pull down public repositories that other people have posted on GitHub so that you can test their programs locally.
If you have any questions, please leave them beneath the video and I will try to get back to you as soon as possible.