如何将 Git 版本控制 (Bitbucket) 添加到现有源代码文件夹?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17570446/
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:31:20  来源:igfitidea点击:

How do I add Git version control (Bitbucket) to an existing source code folder?

gitbitbucket

提问by user1406716

How can I add the contents of an existing folder to Git version control?

如何将现有文件夹的内容添加到 Git 版本控制?

The tutorial herecovers the case of making a directory and then adding source contents to it. I have some source code in a folder that is path dependent and don't want to move it.

此处的教程涵盖了创建目录然后向其中添加源内容的情况。我在一个依赖路径的文件夹中有一些源代码,不想移动它。

So, how can I just go into my folder and make it a repository?

那么,我如何才能进入我的文件夹并使其成为存储库?

回答by Choletski

Final working solutionusing @Arrigo response and @Samitha Chathuranga comment, I'll put all together to build a full response for this question:

使用@Arrigo 回复和@Samitha Chathuranga 评论的最终工作解决方案,我将把所有内容放在一起为这个问题建立一个完整的回复:

  1. Suppose you have your project folder on PC;
  2. Create a new repository on bitbucket: enter image description here

  3. Press on I have an existing project: enter image description here

  4. Open Git CMD console and type command 1 from second picture(go to your project folder on your PC)

  5. Type command git init

  6. Type command git add --all

  7. Type command 2 from second picture (git remote add origin YOUR_LINK_TO_REPO)

  8. Type command git commit -m "my first commit"

  9. Type command git push -u origin master

  1. 假设您在 PC 上有您的项目文件夹;
  2. 在 bitbucket 上创建一个新存储库: 在此处输入图片说明

  3. 我有一个现有项目在此处输入图片说明

  4. 打开 Git CMD 控制台并从第二张图片中键入命令 1(转到 PC 上的项目文件夹)

  5. 键入命令 git init

  6. 键入命令 git add --all

  7. 从第二张图片中键入命令 2 ( git remote add origin YOUR_LINK_TO_REPO)

  8. 键入命令 git commit -m "my first commit"

  9. 键入命令 git push -u origin master

Note: if you get error unable to detect email or name, just type following commands after 5th step:

注意:如果您遇到无法检测电子邮件或姓名的错误,只需在第 5 步后键入以下命令:

 git config --global user.email "yourEmail"  #your email at Bitbucket
 git config --global user.name "yourName"  #your name at Bitbucket

回答by johannes

You can init a Git directory in an directory containing other files. After that you can add files to the repository and commit there.

您可以在包含其他文件的目录中初始化 Git 目录。之后,您可以将文件添加到存储库并在那里提交。

Create a project with some code:

用一些代码创建一个项目:

$ mkdir my_project
$ cd my_project
$ echo "foobar" > some_file

Then, while inside the project's folder, do an initial commit:

然后,在项目文件夹中,执行初始提交:

$ git init
$ git add some_file
$ git commit -m "Initial commit"

Then for using Bitbucket or such you add a remoteand push up:

然后为了使用 Bitbucket 或这样的你添加一个remote并推:

$ git remote add some_name user@host:repo
$ git push some_name

You also might then want to configure tracking branches, etc. See git remote set-branchesand related commands for that.

然后,您可能还想配置跟踪分支等。请参阅git remote set-branches相关命令。

回答by Arrigo

User johannestold you how to do add existing files to a Git repository in a general situation. Because you talk about Bitbucket, I suggest you do the following:

用户johannes告诉您在一般情况下如何将现有文件添加到 Git 存储库。因为你在谈论Bitbucket,我建议你做以下事情:

  1. Create a new repository on Bitbucket (you can see a Create button on the top of your profile page) and you will go to this page:

    Create repository on Bitbucket

  2. Fill in the form, click next and then you automatically go to this page:

    Create repository from scratch or add existing files

  3. Choose to add existing files and you go to this page:

    Enter image description here

  4. You use those commands and you upload the existing files to Bitbucket. After that, the files are online.

  1. 在 Bitbucket 上创建一个新存储库(您可以在个人资料页面顶部看到一个创建按钮),您将转到此页面:

    在 Bitbucket 上创建存储库

  2. 填写表格,点击下一步,然后你会自动进入这个页面:

    从头开始创建存储库或添加现有文件

  3. 选择添加现有文件,然后转到此页面:

    在此处输入图片说明

  4. 您使用这些命令并将现有文件上传到 Bitbucket。之后,文件就在线了。

回答by sver

The commands are given in your Bitbucket account. When you open the repository in Bitbucket, it gives you the entire list of commands you need to execute in the order. What is missing is where exactly you need to execute those commands (Git CLI, SourceTreeterminal).

这些命令在您的 Bitbucket 帐户中给出。当您在 Bitbucket 中打开存储库时,它会为您提供需要按顺序执行的完整命令列表。缺少的是您需要在何处执行这些命令(Git CLI、SourceTree终端)。

I struggled with these commands as I was writing these in Git CLI, but we need to execute the commands in the SourceTree terminal window and the repository will be added to Bitbucket.

我在 Git CLI 中编写这些命令时遇到了这些命令,但我们需要在 SourceTree 终端窗口中执行这些命令,并且存储库将被添加到 Bitbucket。

回答by Kristijan Korman

I have a very simple solution for this problem. You don't need to use the console.

对于这个问题,我有一个非常简单的解决方案。您不需要使用控制台。

TLDR:Create repo, move files to existing projects folder, SourceTree will ask you where his files are, locate the files. Done, your repo is in another folder.

TLDR:创建 repo,将文件移动到现有项目文件夹,SourceTree 会询问您他的文件在哪里,找到文件。完成,您的回购在另一个文件夹中。

Long answer:

长答案:

  1. Create your new repository on Bitbucket
  2. Click "Clone in SourceTree"
  3. Let the program put your new repo where it wants, in my case SourceTree created a new folder in My Documents.
  4. Locate in windows explorer your new repository folder.
  5. Cut the .hg and README (or anything else you find in that folder)
  6. Paste it in the location where is your existing project
  7. Return to SourceTree and it will say "Error encountered...", just click OK
  8. On the left side you will have your repository but with red message: Repository Moved or Deleted. Click on that.
  9. Now you will see Repository Missing popup. Click on Change Folder and locate your existing project folder where you have moved the files mentoned earlier.
  10. Thats it!
  1. 在 Bitbucket 上创建您的新存储库
  2. 单击“在 SourceTree 中克隆”
  3. 让程序将您的新存储库放在它想要的位置,在我的情况下,SourceTree 在我的文档中创建了一个新文件夹。
  4. 在 Windows 资源管理器中找到您的新存储库文件夹。
  5. 剪切 .hg 和 README(或您在该文件夹中找到的任何其他内容)
  6. 将其粘贴到现有项目所在的位置
  7. 返回SourceTree,它会说“遇到错误...”,只需单击“确定”
  8. 在左侧,您将拥有您的存储库,但带有红色消息:存储库已移动或已删除。点击那个。
  9. 现在您将看到 Repository Missing 弹出窗口。单击“更改文件夹”并找到您已将之前提到的文件移动到的现有项目文件夹。
  10. 就是这样!

Tips: Clone in SourceTree option is not available right after you create new repository so you first have to click on Create Readme File for that option to become available.

提示:在创建新存储库后,克隆 SourceTree 选项不可用,因此您首先必须单击“创建自述文件”以使该选项可用。