Microsoft makes it simple to add a new project or an existing project to GitHub with Visual Studio 2019 and the GitHub VS2019 extension. In this post we will review; how to create a local Git repository using Visual Studio 2019, how to create a remote repository on GitHub using the GitHub VS2019 extension and briefly cover some basic source control operations.
Creating Our Local Git Repository
After installing the GitHub Extension for Visual Studio start up a new project or an existing project. Once your project has been created and/or is loaded, look over to the lower right-corner of your project and click “Add to Source Control” -> “Git”. Visual Studio 2019 will automatically create a local Git repository for you and place all necessary files in your solution folder. Most of these files are hidden unless you explicitly have your file explorer show hidden files.
Once our local Git repository has been created, the text will change from from “Add to Source Control” to a helpful little toolbar. Let’s run through what is available to us here briefly. From left to right, we have our pending commits, pending changes denoted with a pencil icon (the number here is the number of files that have been changed but not yet commited), our current working Git repository and our current working branch. (Psst, if you are unfamiliar with any of these words, you may refer to the GitHub glossary.)
You may be wondering why you have two pending commits after you just created your Git repository. These commits were created by VS 2019, one for a couple of our Git files and another for our project files. You can view your commit history by expanding the working branch menu in the toolbar shown above and clicking “View History”. You can also click on any individual commit to view more details in the Team Explorer pane such as the commit’s parent, the files changed and comments written by the commit author.
Lastly, I would like to note that you may notice that not all files in your solution folder are a part of your repository. This is intentional as not all files in your folder are needed to build your project. I recommend taking a brief look at the .gitignore file in your solution folder. This file is pre-filled by VS2019. You can edit the .gitignore file if there is additional items you may not want on source control such as files containing sensitive information. It is worth mentioning, that there appears to be a division in the developer community about whether certain files containing sensitive information should be placed in source control (particularly, remote repositories) and invite you to research the question and act upon your particular scenario and needs.
Creating Our Remote Repository on GitHub
We now have a local Git repository however, it’s not yet backed up on GitHub. We need to create a remote repository on GitHub and sync it to our local repository. Click on the Home button in the Team Explorer pane and select “Sync”.
Now click the “Publish to GitHub” button, fill out the necessary information and click “Publish”. Your repository should now be visible on your GitHub account and the two commits created by VS2019 should have been pushed (along with any additional commits you may have made before creating the remote repository).
Future Changes – Pulling and Pushing
We are now set and ready to begin working with our repository. Let’s briefly review how to add a new commit to our local repository and push it out to our remote repository.
Git will keep track of any changes we make to our source code files. In the Solution Explorer pane in Visual Studio 2019, we can see helpful icons next to our files that show us the state of our file. You can hover above these icons to get a descriptive tool tip.
After making some edits to your source code, head back to the Team Explorer pane, click Home and then Changes. You will see the files that have been altered since your last commit. In order to make a commit, you must enter a description describing the change. Once entered, you will have three options 1) Commit All, 2) Commit All and Push, and 3) Commit All and Sync.
Here’s an overview of each of these options:
- Commit All – A commit will be created locally.
- Commit All and Push – A commit will be created locally and pushed to your remote repository.
- Commit All and Sync – A commit will be will be created locally, then it will sync any additional changes from the remote repository to your local repository and finally your commit will be pushed to your remote repository.
If you are the sole contributor of your project, it is okay to use option two. However, if you are working in a team it’s good practice to use option three to keep your local code base up to date since your teammates may have pushed changes. Alternatively, you may choose to create your commit locally and then push or sync manually through the “Sync” option under Home in the Team Explorer pane.
Whether you choose to commit to your remote repository manually or not, the “Sync” area allows you to push local commits to your remote repository and also check for new commits in the remote repository that may not exist in your local repository. The Incoming Commits section will present any commits made to the repository that are not synced with your local code. You can click “Fetch” to check if there is any commits, and “Pull” to actually bring in changes present in the remote repository that do not exist in your local repository. The Outgoing Commits section will contain any commits you created locally and that have not yet been pushed to our remote repository. Click “Push” to push your commits to your remote repository.
If you are working in a team, it is a good idea to start your day by syncing your code. This allows you to have the most up to date code case and allows you to catch bugs or other problems earlier in the process.
Lastly, I would like to mention there is some source control shortcuts available to you in Visual Studio 2019 when you right-click a file. Most importantly, the “Undo” option will remove all pending changes in a file if you feel you have completely totaled it and want to start new.
You should now be able to use source control at it’s most basic level. If you are still curious about source control in Visual Studio 2019, it only takes a couple of minutes to make a test project with a local and remote repository and play with the source control features available to us in Visual Studio 2019.
Once you feel comfortable with pushing and pulling with your remote repository, I strongly advise learning about and making use of branches even if you feel your project does not require them. You can start by creating a development branch. As a new developer, this will give you experience applicable in real word scenarios where businesses may use several branches. You will also get experience in what can sometimes be the messy process of merging branches.