Add ASP.NET Core projects to GitHub using Visual Studio 2019
If you a beginner in ASP.NET Core, you might have wondered as to how
newly created projects can be placed under a GitHub repository. In this article
we will see the overall steps and configuration to do so. We are not going to
discuss Git or GitHub in much length in such an introductory article. But you
will get an idea about how GitHub can be integrated in Visual Studio 2019. Note
that Visual Studio 2019 is still in a Preview stage. Most of the instructions
discussed in this article are equally applicable to Visual Studio 2017 also. The
UI might be slightly different but the steps remain identical. So, in the
following text when I say Visual Studio I mean any of the two depending on your
installation.
When you wish to add your ASP.NET Core projects under GitHub, there are at
least three possibilities :
- You want to create a new project. And while creating the project you
want to set it up for GitHub.
- You already have a standalone ASP.NET Core project and now you want to
add it to a new GitHub repository.
- You already have an ASP.NET Core project and an empty GitHub repository
and now you want to link the two.
Let's see how each of these possibilities can be handled using Visual Studio.
First of all, go to GitHub
and create a new account. If you already have an account keep the login
credentials ready because you will need them in a minute.
Now open Visual Studio and click on Tools > Extensions and Updates menu
option. In the dialog click on Online on the left side and locate GitHub
Extension for Visual Studio (see below).
Click on the Download button and exit the Visual Studio. Upon exiting, the
installer will be invoked and the GitHub extension will be installed for you.
Next, click on View > Team Explorer to open the team explorer window.
Can you see the GitHub option there? And the Connect button? Click on it to
open the following dialog:
Enter your GitHub login name and password and click on the Sign In button.
Once you successfully connect with GitHub, you can create a new repository to
house your code. To create a repository click on the Create button under GitHub.
Doing so will open the following dialog:
Here, you can specify a name for the repository, a short description and a
local path. Your code is first added to a local repository and it is then pushed
to GitHub repository. The local path points to the path of the local repository.
You can also pick a License to your code. Keep the other settings to their
defaults and click on Create button.
So far so good.
Now, create a new ASP.NET Core project using Visual Studio as you normally
do.
On the next dialog where you specify project name, make sure to create the
project in the folder where the local repository was created earlier. In VS2017 you can check the "Add to Source
Control" checkbox.
Go ahead and select Web Application (MVC) project template as shown below:
And click on OK to create the new project.
The Team Explorer window will now resemble this:
indicating that you are ready to perform source control operations on the
code files.
Click on Changes option. You will be asked to enter user information such as
name and email address
Enter a commit message and click on Commit All option at the top of Team
Explorer.
This will start the commit to the local repository.
Once the commit is successful you will be told about the outcome.
Click on the Sync button to synchronize the local repository with the GitHub
repository.
Click on Push under Outgoing Commits to initiate the task.
Once the push is over, you should see your ASP.NET Core application's code in
the GitHub website.
In the preceding example we added a new project to the repository while
creating it. What if the project is already there and you now want to add it to
a repository?
In that case you can use File > Add to Source Control menu option. Clicking
on this menu option opens Team Explorer as shown below:
Notice the two options there - Publish to GitHub and Publish to Remote
Repository.
The first option can be used to create a new GitHub repository and then add
the current project to it. If you click the Publish to GitHub button you can
create a new repository as before.
To publish to a remote repository go to GitHub website and create a new empty
repository:
Once created enter its URL in the textbox:
Clicking on the Publish button will now add your files to the remote
repository.
That's it for now! Keep coding!!