简体   繁体   English

将Git远程存储库连接到空的本地文件夹

[英]Connect Git remote repository to unempty local folder

Every tutorial and book I've read on Git always address "new and clean" repositories and conditions. 我在Git上阅读的每本教程和书都始终涉及“新的和干净的”存储库和条件。 Mine does not fit this mold. 我的不适合这个模具。

I have a folder holding Eclipse project files (Android app, specifically) called Research. 我有一个文件夹,存放名为Research的Eclipse项目文件(特别是Android应用程序)。 I also have a GitHub repository, also called Research. 我还有一个GitHub存储库,也称为Research。 It has files in its master branch...the very same as the ones I have in my local Research folder. 它的主分支中有文件...与我在本地Research文件夹中的文件完全相同。 It also has 2 additional branches, with more files in them. 它还有2个其他分支,其中包含更多文件。

  1. How do I connect my local Research folder to the remote Research repository? 如何将本地Research文件夹连接到远程Research资源库?

  2. How do I retrieve files within the other branches of this remote repository into my local repository? 如何在此远程存储库的其他分支中将文件检索到本地存储库中?

Thanks! 谢谢!

Okay, I've now tested this and it worked for me, but do make a backup copy of your local Research folder, just in case. 好的,我现在已经对此进行了测试,并且对我有用。 (Or make a copy and try the steps with that copy first.) (或制作副本,然后尝试使用该副本的步骤。)

Based on your comments I'm assuming your local files in Research are exact copies of the ones on the remote. 根据您的评论,我假设您在Research中的本地文件是远程文件的精确副本。 You also said your local Research folder was not a git repository yet/anymore/again, but make sure it really isn't by checking there is no (hidden) .git folder in Research. 您还说过/仍然/再次,您的本地Research文件夹还不是git存储库,但是请通过检查Research中是否没有(隐藏).git文件夹来确保它不是。 If there is, remove it. 如果有,请将其删除。

Now: 现在:

(Re)initialise Research with (重新)初始化研究
git init

next, add your remote (the URL will look different if you use https; 接下来,添加您的遥控器(如果使用https,URL看起来会有所不同;
'origin' is the name we pick to refer to the remote) “起源”是我们用来指代遥控器的名称)
git remote add origin git@github.com:youruser/yourrepo.git

do a fetch of the remote 取得遥控器
git fetch origin

Assuming the main branch of your remote is called 'master' (which is the default), do 假设遥控器的主分支称为“ master”(默认),请执行
git reset --hard origin/master
which will (re)set all tracking info of your local repo to that of your remote. 这会将您本地存储库的所有跟踪信息(重新)设置为远程存储库的所有跟踪信息。

Now if you check which branches you have locally with 现在,如果您检查本地拥有哪些分支
git branch
you should only see your master listed. 您应该只会看到列出的主人。

With
git branch -r
you will see all your remote branches. 您将看到所有远程分支。

Fetch them as needed (lbranchname is the name you give the new local branch; 根据需要获取它们(lbranchname是您赋予新的本地分支的名称;
it might make sense to use the same as the remote branch name rbranchname) 可以使用与远程分支名称rbranchname相同的名称)
git fetch git@github.com:youruser/yourrepo.git lbranchname:rbranchname

If you check 如果您检查
git branch
now, it should list master and lbranchname, to which you can switch with 现在,它应该列出master和lbranchname,您可以使用它们切换到
git checkout lbranchname

... Now work on branches or merge with master as needed. ...现在在分支上工作或根据需要与master合并。

Git doesn't really care about whether your repository is new or not when you add a remote. 当您添加远程服务器时,Git并不真正在乎您的存储库是否是新的。

You just use normal git remote add syntax. 您只使用普通的git remote add语法。

Now, when you go to fetch from the remote, you may get some warning about 'no common commits' if the remote history is completely separate from yours. 现在,当您从远程获取数据时,如果远程历史记录与您的历史记录完全分开,则可能会收到有关“无共同提交”的警告。 How you would proceed depends on whether or not you have local history you want to preserve and a number of other factors. 处理方式取决于您是否要保留本地历史记录以及许多其他因素。

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

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