Using Git In Visual Studio

Posted on  by 



Disclaimer: I’m not a Git guru, so information in this post might not be the most accurate, it just works on my machine and wanted to share my experience.

I take for granted that you are a Visual Studio user, that you use Git using the Visual Studio plugin and, like me, have the need to work on projects where you need to share code hosted in its own separate repository among different solutions.
I know that there are several solutions for this, as example using Nuget packages for shared code, but none of them reaches the flexibility that using source code offers, both in terms of debugging and real time bug fixing.
Ok, not ‘technically correct’ I know, but it works, and I love things that works and make my life easier.
Since I’m not the only one with this need I checked online and found that, among different alternatives, the most voted one (with also several opponents indeed) is to use Git SubModules that, to make it simple, are nothing more than repositories embedded inside a main repository.
In this case, the submodule repository is copied into a subfolder of main one, and information about the original module are also added to main repository this means that when you clone the main project also all submodules are cloned.

Submodules in action
Let’s create our fist project that uses a git submodule, fasten your seat belt and enjoy the journey.

Visual studio install git

I’ve created two UWP projects (but you can use any kind of project of course…) a main UWP application SubModulesApp and a UWP library named SubModulesLib, each one has its own repository hosted on github.com.
I now have the need that SubModulesApp must use some services contained inside SubModules lib and once I start using the lib, is evident that both repos will have a string relationship so, even if i could keep them separated and just reference local SubModulesLib project from main app, the best solution is to create a submodule, this also gives us the benefit to keep submodule on a different commit compared to the ‘master’ one in case we need it.

To set Git as a default source control plug-in for the new solution in Visual Studio 2017, you should do the following: Open Tool Menue Options. At Source Control Plug-in Selection Make sure that the Git. When using Windows, you’ll most likely be using the tools included in the Git for Windows initiative (Git BASH, Git GUI, and Git Shell Integration). Even though Git is completely cross platform, Visual Studio 2019 only works on Windows. In this video, I walk you through the process of using Git with Visual Studio. You will learn how to create a new project with Git, how to connect an existin. Visual Studio 2019 has recently added a new (much-needed) terminal feature in its 16.6 version. It's really great, but I'm having a hard time integrating git bash in the profiles. I've tried a few variations of arguments and whatnot of the following profile. Using Version Control in VS Code Visual Studio Code has integrated source control management (SCM) and includes Git support in-the-box. Many other source control providers are available through extensions on the VS Code Marketplace. Tip: Click on an extension tile to.

Let’s start and open our empty app in Visual Studio:

Visual Studio Connect To Git

Git

Now open a Visual Studio command prompt at solution folder, if you use Visual Studio PowerCommands, just right click the solution node and select Power Command –> Open Command Prompt.
Let now type: git submodule add <Path to your git repository> <foldername> and your project will be cloned into <foldername>

here’s an example of what I’ve got on my machine

and here’s project folder structure

Now you can add the Lib project inside MyAwesomeLib folder to SubModulesApp project

Consume the Lib services and push everything back to github.

Let’s now make an important test: What if lib author updates the code in SubModulesLib? will I get the changes if when I pull SubModulesApp?
To test it I’ve added a comment to MyCalculator.cs class and pushed the change back to original repository, I then pulled the SubModulesApp that uses the lib as submodules and, unfortunately, the change is not there so, it looks like that what we get here is a copy, or, to better say, something not pointing to the latest commit.
To see the changes we need to open the solution from inside our nested folder (in this case MyAwesomeLib) and pull the changes from there, totally annoying stuff that could be avoided if Git plugin for Visual Studio would support multiple repositories (please vote for this feature here: https://visualstudio.uservoice.com/forums/121579-visual-studio-ide/suggestions/8960629-allow-multiple-git-repositories-to-be-active-at-on)
What about the opposite? If I do a modification of code inside a submodule, will it be pushed back to the original repository? (in our case from inside SubModuleApp solution) unfortunately not, as before you need to push changes from the instance of Visual Studio that hosts the SubModuleLib residing inside MyAwesomeLib folder doing that properly aligns the original source repository.

All this works because we are working on the project where submodule was created, if someone else need to clone and work on the same project the following steps must be done:

1-Clone the project from Visual Studio (or manually if you’re an hypster…)
2-Open a VS Command Prompt at solution level and issue this command: git submodule update –init –recursive
3-Open each submodule under main solution, and checkout the associated branch using Visual Studio plugin (you will see that it results in detached state)

Now your cloned solution’s submodules are attached to original repositories and everything works as previously described.

A bit tricky for sure, at least until Visual Studio Git plugin won’t support multiple repositories. but once project is properly initialized is just a matter of remembering to open the submodule project each time you need to interact with git.

Utilize the tight integration of Visual Studio Code with the Git source-control management system.

Learning objectives

In this module, you will:

  • Perform common GitHub tasks by using the Command Palette in Visual Studio.
  • Monitor the status of your work
  • Commit your files to your repositories from the Source Control view

Using Git In Visual Studio Code

Prerequisites

  • Git, installed
  • Visual Studio Code, installed
  • A basic understanding of Git concepts and commands, such as working with repositories, forks, clones, and branches, staging and unstaging changes, and pushing commits.
  • You need a GitHub account.

Visual Studio Install Git

  • Exercise - Clone a repository and create a branchmin
  • Stage, commit, and push changes to a remote repositorymin
  • Exercise - Stage, commit, and push changes to a remote repositorymin




Coments are closed