120

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?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
niico
  • 11,206
  • 23
  • 78
  • 161
  • 2
    You mention "Git in Visual Studio" - are you using the git tools in Visual Studio 2012? – Edward Thomson May 30 '13 at 17:56
  • I believe this has now been embedded in the 2015 version under Team Explorer. I ran into this, where i couldnt 'Publish' (vs's initial push after init), so i will add an answer with instructions on adding the URI – mschr Nov 08 '16 at 17:06

18 Answers18

171

I went searching around for a similar question - the way I've managed to initialize a Git repository for an existing project file is this (disclaimer: this is done in Visual Studio 2013 Express, without a Team Foundation Server setup):

  1. Open the project in Visual Studio.
  2. Go to menu FileAdd to Source Control.

That did it for me - assuming Git is set up for you, you can go to menu ViewTeam Explorer, then double click the repository for your project file, and make your initial commit (making sure to add whatever files you'd like).

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
airbear
  • 1,842
  • 1
  • 12
  • 3
  • 9
    This will also save you the hassle to "supply a reasonable .gitignore file", as Visual Studio will take care of this detail for you. Between a manual git init and this, this is my preferred method from now on. – MrCC Jun 20 '15 at 18:46
  • If you would like to chose the Project this new solution goes into, you can click the Sync button - then click publish to git repo, then click advanced solution and pick the Project you would like to add this new solution to.. once you have done that, you can then sync and boom – Jamisco Jul 28 '18 at 01:18
  • 1
    Note: that this didn't work for me because I had already added a shared library that was included in another repository. I had to remove the shared project before the 'Add to Source Control' option became available. – Neil B Sep 26 '18 at 18:03
  • 3
    They should add this to Team Explorer window. – Persk Aug 15 '19 at 04:46
  • 1
    This should not be releavant anymore... int 2020 – user1034912 Sep 23 '20 at 12:36
  • Persk's comment is still in a problem with 2022, even after the "multi git" update. – Mmm Dec 23 '22 at 00:41
72
  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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Klas Mellbourn
  • 42,571
  • 24
  • 140
  • 158
17

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Helpful
  • 189
  • 1
  • 2
16

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Hashem Aboonajmi
  • 13,077
  • 8
  • 66
  • 75
  • This answer is correct and works in VS 2015. However I had a major question with this being a new git user. VS 2015 did not seem to create a gitignore file or offer any way to ignore dlls/files that are created by VS that should NOT be checked in. Like the obj folder for instance and it is trying to commit everything into the local repos. Anyone else have this issue? I dont want all this stuff committed locally. – Matt Nov 12 '17 at 15:06
  • I'll amend my comment above as I learn more. When i check the directory it did in fact build the gitignore file which is great because it is very large and I would hate to try to build this manually. However, when i tried to do my initial commit into the local repo, VS did not use this gitignore file because all files were available for committing into git and it was a big mess. So i closed the solution, deleted the .git folder and 2 git files in my solution (which detaches from git), reopened and went to file->add to source control and selected git again. This time it worked great. – Matt Nov 12 '17 at 15:28
  • It is now Dec 2019 and it is even easier. After I did the steps above and hit Sync, I was prompted with the GitHub account and whether I wanted to make this a private repository. Once selected, the new repository was created for me (without me having to go to GitHub) and created successfully. – batpox Dec 08 '19 at 09:04
6

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Emil
  • 6,411
  • 7
  • 62
  • 112
5

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 RepositoriesAdd Remote.... Paste your URL in that box and hit OK.

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Clark
  • 2,598
  • 3
  • 27
  • 41
4

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.

chavab_1
  • 237
  • 2
  • 10
  • After you select "File -> Add to Source Control", is there anything in the Output window for "Source Control - Git" pane? I wouldn't expect anything in the Changes page, but if you viewed history of the Git repository, I would expect it to show a commit "Add project files" where the files were committed to the repository. – jamill Feb 01 '17 at 22:14
  • Is this the same as the Source Control Explorer? If so, it doesn't show anything. It just displays a link to add a source control plugin. When I click it, and select Git, nothing happens. As for the other panels inside Team Explorer, nothing happens either. It's only after I initialize the project from the command line and then create a local repo from the Connections pane that I start to see VS Git tools interacting with my project. – chavab_1 Feb 01 '17 at 22:47
2

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
2

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

Alexei - check Codidact
  • 22,016
  • 16
  • 145
  • 164
2

I have followed the below steps to add or export an existing project to GitHub and it should work for all the environments.

  1. Create a new Repo in GitHub then copy the repo URL
  2. Open the terminal in VS or Gitbash
  3. You should be in the project path / working directory
  4. git init --to initialize git on local directory
  5. git add . -- add your project
  6. git commit -m "YOUR MESSAGE"
  7. git remote add origin
  8. git remote -v
  9. git push -f origin master -- to push your changes to Git repo
Pushparaj
  • 92
  • 4
1

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Jiminion
  • 5,080
  • 1
  • 31
  • 54
1

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
mschr
  • 8,531
  • 3
  • 21
  • 35
1

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.

RetroCoder
  • 2,597
  • 10
  • 52
  • 81
1

In Visual Studio 2022 It looks like there is no option "Add to source control".

  • Go to "Git Changes"

enter image description here

  • Click "Create Git Repository..."
  • Click "Existing remote" Enter Remote URL enter image description here
SamBerk
  • 311
  • 2
  • 10
0

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Andrew_STOP_RU_WAR_IN_UA
  • 9,318
  • 5
  • 65
  • 101
0

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.).

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
testing
  • 19,681
  • 50
  • 236
  • 417
0

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Cool Blue
  • 6,438
  • 6
  • 29
  • 68
0

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://yourusername@bitbucket.org/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.

zar
  • 11,361
  • 14
  • 96
  • 178