[git] Git in Visual Studio - add existing project?

I'm trying to put an existing project under Git source control, but I'm unclear on several things.

I have set up a 'Team Foundation Service' Git account online.

I currently have an ASP.NET MVC 4 solution - in my Projects folder. I have created a Git 'repository' locally (an empty folder currently). Should this just be my current application folder in Projects? Or does it need to be a copy?

How do I get my existing files into the online repository?

The answer is


If the desired repository already exists (perhaps on GitHub) then you can clone it to your local system, and then copy the solution directory into it. Then add the files, commit the files, and push the local. This put the solution in a repository.


After slogging around Visual Studio I finally figured out the answer that took far longer than it should have.

In order to take an existing project without source control and put it to an existing EMPTY (this is important) GitHub repository, the process is simple, but tricky, because your first inclination is to use the Team Explorer, which is wrong and is why you're having problems.

First, add it to source control. There are some explanations of that above, and everybody gets this far.

Now, this opens an empty LOCAL repository and the trick which nobody ever tells you about is to ignore the Team Explorer completely and go to the Solution Explorer, right click the solution and click Commit.

This then commits all differences between your existing solution and the local repository, essentially updating it with all these new files. Give it a default commit name 'initial files' or whatever floats your boat and commit.

Then simply click Sync on the next screen and drop in the EMPTY GitHub repository URL. Make sure it's empty or you'll have master branch conflicts and it won't let you. So either use a new repository or delete the old one that you had previously screwed up. Bear in mind this is Visual Studio 2013, so your mileage may vary.


On Visual Studio 2015 the only way I finally got it to work was to run git init from the root of my directory using the command line. Then I went into Team Explorer and added a local git repository. Then I selected that local git repository, went to Settings->Repository Settings, and added my Remote Repo. That's how I was finally able to integrate Visual Studio to use my existing project with git.

I read all of the answers but none of them worked for me. I went to File->Add To Source Control, which was suppose to basically do the same as git init, but it didn't seem to initialize my project because when I would then go to Team Explorer all of the options were grayed out. Also nothing would show up in the Changes dialog either. Another answer stated that I just had to create a local repo in Team Explorer and then my changes would show up, but that didn't work either. All the Git options on Team Explorer only worked after I initialized my project through the command line.

I'm new to Visual Studio so I don't know if I just missed something obvious, but it seems like my project wasn't initializing from Visual Studio.


First create a 'Solution Folder' with the desired relative path. Note that Visual Studio 2012 does not create a system folder with the same relative path.

Now inside that 'Solution Folder' add a new project, but you must be careful when defining it that the relative path in the system matches the relative path of your new 'Solution Folder'. If the system folder you want does not exist, Visual Studio 2012 will now create it for the new project. (As noted above, it does not do this when you add a new 'Solution Folder'.)

If you want to add an existing file with the matching relative path, you must first create the file in the matching system relative path, from outside of Visual Studio. Then you can Add existing file in Visual Studio.


Just right click on your solution and select Add to source control. Then select Git.

Now your projects has been added to a local source control. Right click on one of your files and select Commit.

Then enter a commit message and select Commit. Then select Sync to synchronise your project with GitHub. It requires having a Git repository. Go to GitHub, create a new repository, copy the repository link, and add it to your remote source control server. Select Publish.

That's all.


Git in Visual Studio - add existing project; how to publish your local repository to a project on GitHub, GitLab, or the like.

So, you have created a solution and you want it uploaded and versioncontroller via your Git account somewhere. Visual Studio 2015 has tools in Team Explorer for this.

As Meuep mentions, load your solution and then navigate File >> Add to Source Control. This is the equivalent of git init. Then you will have this:

Team Explorer Home

Now, select Settings >> Repository Settings and scroll to Remotes.

Enter image description here

Set up origin (make sure you put this reserved name there) and set URIs.

Then you may use Add, Sync and Publish.


Using SourceTree:

Right click the solution name in Solution Explorer. Select "Add to Source Control".

Then head over to SourceTree, and select Clone/New. Select Add Working Folder and point to the new Git place you just made inside your solution.

Go and grab the clone address for an empty Git, (Bitbucket or GitHub) and go back to SourceTree, right click Remotes and add New Remote. (Note in modern versions, go to Repositories ? Add Remote.... Paste your URL in that box and hit OK.

This is how you can make your initial commit and push.


The easiest way is obviously as described in the MSDN article Share your code with Visual Studio 2017 and VSTS Git.

  1. Create a new local Git repository for your project by selecting Add to Source Control in the status bar in the lower right hand corner of Visual Studio. This will create a new repository in the folder the solution is in and commit your code into that repository.

    Enter image description here

  2. In the Push view in Team Explorer, select the Publish Git Repository button under Push to Visual Studio Team Services.

    Enter image description here

  3. Connect Remote Source Control and enter your repository name and select Publish Repository.

    Enter image description here


If you want to open an existing project from GitHub, you need to do the following (these are steps only for Visual Studio 2013!!!! And newer, as in older versions there are no built-in Git installation):

  1. Team explorer ? Connect to teamProjects ? Local GitRepositories ? Clone.

  2. Copy/paste your GitHub address from the browser.

  3. Select a local path for this project.


I always forget this so this is more for myself but maybe might help someone else using VS.

Visual Studio has the concept of Solutions. In git however, it has the concept of a git repository tracked by local and remote branches. All the files and folders that get added to git are local.

Now going back to Visual Studio Solutions when you create a standard template project it creates all projects locally to that solution.

So the problem happens when you add a project that is not local to the solution or to git for that matter. What happens is that the solution file .sln is updated with the project location but the actual contents of the project, project files and folders can not be added to git because they are on a separate directory or a separate network drive or one some ftp server etc.... This might be preferable if you only want a reference to a project file that gets compiled in the .sln file only or you want to source them to separate git or github repositories. But you don't want the actual files to be tracked by git locally.

To remedy this (i.e. you want to add them to your git repository) you just move the remote files of interest within the purview of the solution and its projects files so that they are local.


For me a Git repository (not GitHub) was already created, but empty. This was my solution for adding the existing project to the Git repository:

  1. I cloned and checked out a repository with Git Extensions.
  2. Then I copied my existing project files into this repository.
  3. Added a .gitignore file.
  4. Staged and committed all files.
  5. Pushed onto the remote repository.

It would be interesting to see this all done directly in Visual Studio 2015 without tools like Git Extensions, but the things I tried in Visual Studio didn't work (Add to source control is not available for my project, adding a remote didn't help, etc.).


To add a project within a solution, just open the Team Explorer window and go to Changes. Then, under Untracked Files, click on View options and select Switch to Tree View (unless it is already in tree view), right click on the project root folder, and select Add.

Enter image description here


Here is how to do it in Visual Studio 2015.

  1. Open the project and go to Tools >> Options >> "Source Control" tab and select "Git" as your source control. enter image description here

  2. Go to File Menu and select "Add to source control".

What this does is essentially execute "git init" by visual studio creating all the bells and whistles (setup ignore files accordingly like ignore user specific files, temporary files, directory etc).

Essentially your git repository has been created correct locally (this is the major step with VS project since this folder contains a lot of files that we want to ignore).

Now you can connect this git repository to any other git like GitHub, BitBucket etc in git bash (outside Visual Studio).

I start Git Bash session now in this working directory and since I use BitBucket, I follow the instructions given there (just copy the lines below from there).

git remote add origin https://[email protected]/yourusername/hookdemo.git

git push -u origin master

That's it.

Afterwards you can manage the repository on git command line itself since its setup properly now.


  1. First of all you need to install Git software on your local development machine, e.g. Git Extensions.
  2. Then do git init in the solution folder. That is the proper way to create a repository folder.
  3. Set up a reasonable .gitignore file, so you don't commit unnecessary stuff.
  4. git add
  5. git commit
  6. Add the proper remote, as described in your Team Foundation Server account git remote add origin <proper URL>
  7. git push your code

Alternatively, there are detailed guides here using the Visual Studio integration.


The process is greatly simplified for VS2017 / VS2019 (maybe even earlier, but I have not tested) in conjunction with GitHub:

  1. Create an empty repository in GitHub => https://github.com//.git

  2. Install GitHub extension for Visual Studio

  3. Follow these instructions: (optionally add to source control to git initialize) -> Team explorer -> Sync -> Publish to GitHub -> https://github.com//.git


Examples related to git

Does the target directory for a git clone have to match the repo name? Git fatal: protocol 'https' is not supported Git is not working after macOS Update (xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools) git clone: Authentication failed for <URL> destination path already exists and is not an empty directory SSL_connect: SSL_ERROR_SYSCALL in connection to github.com:443 GitLab remote: HTTP Basic: Access denied and fatal Authentication How can I switch to another branch in git? VS 2017 Git Local Commit DB.lock error on every commit How to remove an unpushed outgoing commit in Visual Studio?

Examples related to visual-studio-2012

How to actually search all files in Visual Studio Tests not running in Test Explorer How to use _CRT_SECURE_NO_WARNINGS Could not load file or assembly 'Microsoft.ReportViewer.Common, Version=11.0.0.0 How can I resolve the error: "The command [...] exited with code 1"? Could not load file or assembly Exception from HRESULT: 0x80131040 MSVCP120d.dll missing How can I change IIS Express port for a site The program can't start because MSVCR110.dll is missing from your computer Controlling execution order of unit tests in Visual Studio

Examples related to visual-studio-2008

Convert Text to Uppercase while typing in Text box SQL Server r2 installation error .. update Visual Studio 2008 to SP1 What is and how to fix System.TypeInitializationException error? Error LNK2019: Unresolved External Symbol in Visual Studio download and install visual studio 2008 Git in Visual Studio - add existing project? Visual Studio can't 'see' my included header files How to insert Records in Database using C# language? If statements for Checkboxes LNK2019: unresolved external symbol _main referenced in function ___tmainCRTStartup

Examples related to tfs

git clone: Authentication failed for <URL> How to connect TFS in Visual Studio code .net Core 2.0 - Package was restored using .NetFramework 4.6.1 instead of target framework .netCore 2.0. The package may not be fully compatible The target principal name is incorrect. Cannot generate SSPI context Git in Visual Studio - add existing project? How to remove a TFS Workspace Mapping? How do I get my solution in Visual Studio back online in TFS? Error TF30063: You are not authorized to access ... \DefaultCollection Source file 'Properties\AssemblyInfo.cs' could not be found How to get tf.exe (TFS command line client)?

Examples related to azure-devops

Visual Studio 2017 - Git failed with a fatal error Authentication failed for https://xxx.visualstudio.com/DefaultCollection/_git/project Git in Visual Studio - add existing project? Error TF30063: You are not authorized to access ... \DefaultCollection How can I change the default credentials used to connect to Visual Studio Online (TFSPreview) when loading Visual Studio up? Free Online Team Foundation Server