简体   繁体   中英

How to add a project to source control (existing GIT remote repo) in visual studio?

I have a solution (solution 1) that contains a project (project 1). This project was added to source control and I do all git operations normally. I had another solution with another project (project 2) that had a different repo.

I want to merge these 2 solutions and put them under 1 solution only. So, I right clicked on my solution in solution 1 and added existing project (project 2) to it. My problem is that the git repo on azure devops can't see that I added this project to the solution so basically I can't commit or revert or do any git operation. How can I fix that? I need to add this project to source control but when I right click it, add to source control is not there.

Thanks in advance

Think of a local git repo as encompassing a parent folder. It includes all the (tracked) files in that folder or sub-folders.

In your Solution, there can be references that point to other folders, outside of that git repo. In fact, project/solution 2 already has a separate git repo, so that tells me it is in a different folder.

You have a few ways of resolving this, but to start with, you mostly likely want to remove project 2 from solution 1 to start with. Then decide how you want to add project 2 to solution 1.



Just leave them separate repos!

The easiest response is actually just to leave them separate repos. You can change them independently, and update them in their own repos - however, as you have noticed, there are likely some problems with this if you are using VS's native git interface - I think you would have to swap between solutions.

The big downside of this is that git won't track which version of project 2 is being used at any given commit of solution 1 automatically. You would need a version explicitly indicated somewhere (I think it probably is somewhere in the solution config though?). I wouldn't rely on the solution configuration to track the underlying versioning for you - much better to have it all solidly tracked directly by git. From a version control system perspective, the other solutions are better.



Just copy the folder over

The simplest solution that manages project 2 versioning is to just copy the project 2 folder from it's own folder / git repo into the folder / git repo for solution 1. First remove it from your solution, then copy it over, and readd project 2 from the new location.

After doing this, you can add it as a new project in solution 1, and all the links to it will be to this local copy.

However, the downside is that now solution 1/project 2 and the original project 2 in its separate repo are diverging. If project 2 is intended only for solution 1 at this point, that's probably fine.



Make project 2 a submodule

The slightly more complex solution is to make the repo for project 2 a submodule of the repo for solution 1. This adds it as a folder within your solution 1 folder hierarchy, so it looks just like the prior solution, but there is a little extra work to understand how to update the submodule.

Basically, under this setup, the separate git repo is added with a reference to the correct commit in that repo to use at any given time. You can update repo 2 completely separately, or can change it directly in the submodule, make new commits, etc., but they are separate repos, with git keeping track of which version of repo 2 (which commit) is being used at all times, so the version control is automatic.

Last time I used Visual Studio, I do not believe it's native git interface had support for sub-modules. I'm not sure if it does now. You may need to learn how to manage them at the command line or with a separate visual GUI Git tool, like GitExtensions (on Windows only, for v3+).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM