Visual Studio 中的 Git - 添加现有项目?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16824994/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 16:17:10  来源:igfitidea点击:

Git in Visual Studio - add existing project?

gitvisual-studio-2012visual-studio-2008tfsazure-devops

提问by niico

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

我正在尝试将现有项目置于 Git 源代码控制之下,但我不清楚有几件事。

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

我已经在线设置了一个“团队基础服务”Git 帐户。

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?

我目前有一个 ASP.NET MVC 4 解决方案 - 在我的 Projects 文件夹中。我在本地创建了一个 Git“存储库”(当前是一个空文件夹)。这应该只是我当前在 Projects 中的应用程序文件夹吗?还是必须是副本?

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

如何将现有文件放入在线存储库?

回答by airbear

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

我四处寻找类似的问题 - 我设法为现有项目文件初始化 Git 存储库的方式是这样的(免责声明:这是在 Visual Studio 2013 Express 中完成的,没有安装 Team Foundation Server):

  1. Open the project in Visual Studio.
  2. Go to menu FileAdd to Source Control.
  1. 在 Visual Studio 中打开项目。
  2. 转到菜单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).

为我做到了 - 假设为您设置了 Git,您可以转到菜单ViewTeam Explorer,然后双击您的项目文件的存储库,并进行初始提交(确保添加您想要的任何文件)。

回答by Klas Mellbourn

  1. First of all you need to install Git software on your local development machine, e.g. Git Extensions.
  2. Then do git initin the solution folder. That is the proper way to create a repository folder.
  3. Set up a reasonable .gitignorefile, 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 pushyour code
  1. 首先,您需要在本地开发机器上安装 Git 软件,例如Git Extensions
  2. 然后git init在解决方案文件夹中执行。这是创建存储库文件夹的正确方法。
  3. 设置一个合理的.gitignore文件,这样你就不会提交不必要的东西。
  4. git add
  5. git commit
  6. 添加适当的远程,如您的 Team Foundation Server 帐户中所述 git remote add origin <proper URL>
  7. git push你的代码

Alternatively, there are detailed guides hereusing the Visual Studio integration.

另外,还有一些详细指南这里使用Visual Studio集成。

回答by Helpful

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

在 Visual Studio 上苦苦挣扎后,我终于找到了比它应该花费的时间要长得多的答案。

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.

为了将没有源代码控制的现有项目放入现有的 EMPTY(这很重要)GitHub 存储库,过程很简单,但很棘手,因为您的第一个倾向是使用团队资源管理器,这是错误的,这就是为什么你有问题。

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.

现在,这会打开一个空的 LOCAL 存储库,并且没有人告诉您的技巧是完全忽略团队资源管理器并转到解决方案资源管理器,右键单击解决方案并单击提交。

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

然后只需单击下一个屏幕上的同步并放入 EMPTY GitHub 存储库 URL。确保它是空的,否则你会有主分支冲突,它不会让你。因此,要么使用新的存储库,要么删除您之前搞砸的旧存储库。请记住,这是 Visual Studio 2013,因此您的里程可能会有所不同。

回答by Hashem Aboonajmi

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

只需右键单击您的解决方案并选择Add to source control。然后选择 Git。

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

现在您的项目已添加到本地源代码管理中。右键单击您的文件之一并选择Commit

Then enter a commit message and select Commit. Then select Syncto 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.

然后输入提交消息并选择Commit。然后选择同步以将您的项目与 GitHub 同步。它需要有一个 Git 存储库。转到 GitHub,创建一个新存储库,复制存储库链接,并将其添加到您的远程源代码控制服务器。选择发布

That's all.

就这样。

回答by Clark

Using SourceTree:

使用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 Folderand point to the new Git place you just made inside your solution.

然后转到 SourceTree,并选择Clone/New。选择添加工作文件夹并指向您刚刚在解决方案中创建的新 Git 位置。

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

去获取一个空 Git 的克隆地址(Bitbucket 或 GitHub),然后返回 SourceTree,右键单击Remotes并添加New Remote。(注意在现代版本中,转到RepositoriesAdd Remote...。将您的 URL 粘贴到该框中并点击OK

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

这是您进行初始提交和推送的方式。

回答by chavab_1

On Visual Studio 2015 the only way I finally got it to work was to run git initfrom 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.

在 Visual Studio 2015 上,我最终让它工作的唯一方法是git init使用命令行从我的目录的根目录运行。然后我进入 Team Explorer 并添加了一个本地 git 存储库。然后我选择了本地 git 存储库,转到设置->存储库设置,并添加了我的远程存储库。这就是我最终能够集成 Visual Studio 以使用我现有的项目和 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.

我阅读了所有答案,但没有一个对我有用。我去了文件->添加到源代码管理,这应该与 基本相同git init,但它似乎没有初始化我的项目,因为当我转到团队资源管理器时,所有选项都变灰了。更改对话框中也不会显示任何内容。另一个答案指出,我只需要在 Team Explorer 中创建一个本地存储库,然后我的更改就会显示出来,但这也不起作用。团队资源管理器上的所有 Git 选项仅在我通过命令行初始化我的项目后才起作用。

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.

我是 Visual Studio 的新手,所以我不知道我是否只是错过了一些明显的东西,但我的项目似乎不是从 Visual Studio 初始化的。

回答by batmaci

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

最简单的方法显然是 MSDN 文章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 Controlin 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

  1. 通过在 Visual Studio 右下角的状态栏中选择添加到源代码管理,为您的项目创建一个新的本地 Git 存储库。这将在解决方案所在的文件夹中创建一个新存储库,并将您的代码提交到该存储库中。

    在此处输入图片说明

  2. 在 Team Explorer 的 Push 视图中,选择 Push to Visual Studio Team Services 下的 Publish Git Repository 按钮。

    在此处输入图片说明

  3. 连接 Remote Source Control 并输入您的存储库名称并选择Publish Repository

    在此处输入图片说明

回答by Anthony Biagioli

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.

首先使用所需的相对路径创建一个“解决方案文件夹”。请注意,Visual Studio 2012 不会创建具有相同相对路径的系统文件夹。

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

现在在“解决方案文件夹”中添加一个新项目,但在定义它时必须小心,确保系统中的相对路径与新“解决方案文件夹”的相对路径相匹配。如果所需的系统文件夹不存在,Visual Studio 2012 现在将为新项目创建它。(如上所述,当您添加新的“解决方案文件夹”时,它不会这样做。)

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 filein Visual Studio.

如果要添加具有匹配相对路径的现有文件,必须首先从 Visual Studio 外部在匹配的系统相对路径中创建文件。然后您可以在 Visual Studio 中添加现有文件

回答by Jiminion

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.

如果所需的存储库已经存在(可能在 GitHub 上),那么您可以将其克隆到本地系统,然后将解决方案目录复制到其中。然后添加文件,提交文件,然后推送本地。这将解决方案放在存储库中。

回答by testing

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:

对我来说,已经创建了一个 Git 存储库(不是 GitHub),但它是空的。这是我将现有项目添加到 Git 存储库的解决方案:

  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 .gitignorefile.
  4. Staged and committed all files.
  5. Pushed onto the remote repository.
  1. 我使用 Git 扩展克隆并签出了一个存储库。
  2. 然后我将我现有的项目文件复制到这个存储库中。
  3. 添加了一个.gitignore文件。
  4. 暂存并提交所有文件。
  5. 推送到远程存储库。

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 controlis not available for my project, adding a remote didn't help, etc.).

看到这一切都直接在 Visual Studio 2015 中完成而没有 Git 扩展之类的工具会很有趣,但是我在 Visual Studio 中尝试的事情没有奏效(添加到源代码控制对我的项目不可用,添加远程没有帮助等)。