简体   繁体   English

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

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

How can I add the contents of an existing folder to Git version control? 如何将现有文件夹的内容添加到Git版本控制?

The tutorial here covers 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? 那么,我怎样才能进入我的文件夹并使其成为存储库?

Final working solution using @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; 假设您在PC上有项目文件夹;
  2. Create a new repository on bitbucket: 在bitbucket上创建一个新的存储库: 在此输入图像描述

  3. Press on I have an existing project : 我有一个现有的项目 在此输入图像描述

  4. Open Git CMD console and type command 1 from second picture(go to your project folder on your PC) 打开Git CMD控制台并从第二张图片中键入命令1(转到PC上的项目文件夹)

  5. Type command git init 输入命令git init

  6. Type command git add --all 输入命令git add --all

  7. Type command 2 from second picture ( git remote add origin YOUR_LINK_TO_REPO ) 从第二张图片输入命令2( git remote add origin YOUR_LINK_TO_REPO

  8. Type command git commit -m "my first commit" 输入命令git commit -m "my first commit"

  9. Type command git push -u origin master 输入命令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

You can init a Git directory in an directory containing other files. 您可以在包含其他文件的目录中初始化Git目录。 After that you can add files to the repository and commit there. 之后,您可以将文件添加到存储库并在那里提交。

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 remote and 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-branches and related commands for that. 您也可能希望配置跟踪分支等。请参阅git remote set-branches及其相关命令。

User johannes told you how to do add existing files to a Git repository in a general situation . 用户johannes告诉您如何在一般情况下将现有文件添加到Git存储库 Because you talk about Bitbucket, I suggest you do the following: 因为你谈论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: 在Bitbucket上创建一个新的存储库(您可以在个人资料页面的顶部看到一个“创建”按钮),然后您将转到此页面:

    在Bitbucket上创建存储库

  2. Fill in the form, click next and then you automatically go to this page: 填写表单,单击“下一步”,然后自动转到此页面:

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

  3. Choose to add existing files and you go to this page: 选择添加现有文件,然后转到此页面:

    在此输入图像描述

  4. You use those commands and you upload the existing files to Bitbucket. 您可以使用这些命令并将现有文件上载到Bitbucket。 After that, the files are online. 之后,文件在线。

The commands are given in your Bitbucket account. 这些命令在您的Bitbucket帐户中给出。 When you open the repository in Bitbucket, it gives you the entire list of commands you need to execute in the order. 当您在Bitbucket中打开存储库时,它会为您提供需要在订单中执行的完整命令列表。 What is missing is where exactly you need to execute those commands (Git CLI, SourceTree terminal). 缺少的是您需要执行这些命令的位置(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。

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. TLDR:创建仓库 ,将文件移动到现有项目文件夹,SourceTree会询问他的文件在哪里,找到文件。 Done, your repo is in another folder. 完成后,您的仓库位于另一个文件夹中。

Long answer: 答案很长:

  1. Create your new repository on Bitbucket 在Bitbucket上创建新的存储库
  2. Click "Clone in SourceTree" 点击“在SourceTree中克隆”
  3. Let the program put your new repo where it wants, in my case SourceTree created a new folder in My Documents. 让程序把你的新repo放到它想要的地方,在我的例子中,SourceTree在My Documents中创建了一个新文件夹。
  4. Locate in windows explorer your new repository folder. 在Windows资源管理器中找到新的存储库文件夹。
  5. Cut the .hg and README (or anything else you find in that folder) 剪切.hg和README(或在该文件夹中找到的任何其他内容)
  6. Paste it in the location where is your existing project 将其粘贴到现有项目所在的位置
  7. Return to SourceTree and it will say "Error encountered...", just click OK 返回SourceTree,它会说“遇到错误...”,只需单击“确定”即可
  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. 现在您将看到Repository Missing弹出窗口。 Click on Change Folder and locate your existing project folder where you have moved the files mentoned earlier. 单击“更改文件夹”,找到您之前已移动文件的现有项目文件夹。
  10. Thats it! 而已!

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中的克隆选项不可用,因此您首先必须单击“创建自述文件”才能使该选项可用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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