如何将github存储库的内容添加到另一个github存储库?

如何将github存储库的内容添加到另一个github存储库?

问题描述:

昨天我创建了一个github仓库,今天我发现我的顾问还创建了一个github仓库。将我的存储库添加到他的存储库的最佳方式是什么,而不会大量地使用它?过去我已经大量搞砸了,所以我只想确定一下。此外,我可能已经通过尝试按照另一个StackOverflow帖子上的说明搞砸了。如何将github存储库的内容添加到另一个github存储库?

现在好像我有两个分支,并输入“git checkout master”给我我的文件,而输入“git checkout tmp_branch”给我他的文件。我想将所有文件添加到他的存储库,并从现在开始使用该文件,并可能删除我的存储库。

到目前为止我所试图做的是

[email protected]:~/rootdir$ git remote add origin https://github.com/him/his_repo.git 
fatal: remote origin already exists. 
[email protected]:~/rootdir$ git remote add his_repo https://github.com/him/his_repo.git 
[email protected]:~/rootdir$ git push -u his_repo master 
Username for 'https://github.com': [email protected] 
Password for 'https://[email protected]@github.com': 
To https://github.com/him/his_repo.git 
! [rejected]  master -> master (non-fast-forward) 
error: failed to push some refs to 'https://github.com/him/his_repo.git' 
hint: Updates were rejected because the tip of your current branch is behind 
hint: its remote counterpart. Merge the remote changes (e.g. 'git pull') 
hint: before pushing again. 
hint: See the 'Note about fast-forwards' in 'git push --help' for details. 
[email protected]:~/rootdir$ git pull his_repo 
Username for 'https://github.com': [email protected] 
Password for 'https://[email protected]@github.com': 
warning: no common commits 
remote: Counting objects: 13, done. 
remote: Compressing objects: 100% (10/10), done. 
remote: Total 13 (delta 0), reused 10 (delta 0) 
Unpacking objects: 100% (13/13), done. 
From https://github.com/him/his_repo 
* [new branch]  master  -> his_repo/master 
You asked to pull from the remote 'his_repo', but did not specify 
a branch. Because this is not the default configured remote 
for your current branch, you must specify a branch on the command line. 
[email protected]:~/rootdir$ git checkout -b tmp_branch his_repo/master 
warning: unable to rmdir mysubdir: Directory not empty 
Branch tmp_branch set up to track remote branch master from repo. 
Switched to a new branch 'tmp_branch' 
[email protected]:~/rootdir$ git checkout -b origin/master 
Switched to a new branch 'origin/master' 
[email protected]:~/rootdir/subdir$ git checkout -b master 
fatal: A branch named 'master' already exists. 
[email protected]:~/rootdir/subdir$ git checkout master 
Checking out files: 100% (2409/2409), done. 
Switched to branch 'master' 
[email protected]:~/rootdir$ git checkout tmp_branch 
warning: unable to rmdir mysubdir: Directory not empty 
Switched to branch 'tmp_branch' 
[email protected]:~/rootdir$ git mv * tmp_branch/* 
fatal: destination 'tmp_branch/*' is not a directory 
[email protected]:~/rootdir$ cd .. 
[email protected]:~/rootdir$ git checkout master 
Checking out files: 100% (2409/2409), done. 
Switched to branch 'master' 
[email protected]:~/rootdir$ git push -u tmp_branch master 
fatal: 'tmp_branch' does not appear to be a git repository 
fatal: The remote end hung up unexpectedly 
[email protected]:~/rootdir$ git push -u his_repo master 
Username for 'https://github.com': [email protected] 
Password for 'https://[email protected]@github.com': 
To https://github.com/him/his_repo.git 
! [rejected]  master -> master (non-fast-forward) 
error: failed to push some refs to 'https://github.com/him/his_repo.git' 
hint: Updates were rejected because the tip of your current branch is behind 
hint: its remote counterpart. Merge the remote changes (e.g. 'git pull') 
hint: before pushing again. 
hint: See the 'Note about fast-forwards' in 'git push --help' for details. 

非常感谢:)

+1

您的意思是“将我的存储库添加到他的存储库”是什么意思?你在找什么结果? – Chris 2014-10-26 20:25:01

+1

理想情况下,会有一个存储库(他的),我会将所有的数据添加到它。我最终做的是将我的数据复制到一个临时文件夹,删除我的整个存储库,将他的数据放入一个新文件夹,然后将我的数据移动到新文件夹。 – Jessica 2014-10-26 21:37:49

+0

有可能是一个更好的方法来做到这一点,但我是一个n00b。 – Jessica 2014-10-26 21:38:38

为了让您的顾问的存储库包含文件,以及关于你的资料库忘记:

  1. 将他的存储库添加为另一个远程文件
  2. 检出一个跟踪其主分支的新本地分支
  3. 使该分支上,取代他的档案与你
  4. 推分支到他的远程
  5. 从本地资源库中删除旧的远程一个新的提交(并删除它在远程服务器上,如果你想)

在您的shell命令日志中,您已经完成了步骤1和步骤4。步骤4失败是因为您没有执行步骤2和步骤3.当您在步骤4中尝试将提交历史推送到其远程位置时,您位于旧存储库的分支上,该分支具有完全不同的历史记录。 Git拒绝这样做,因为你会用自己的历史覆盖他所有的历史。为了解决这个问题,通过检查他的分支开始他的历史,然后添加一个新的提交历史,用你的文件替换他的文件。

我没有记住确切git命令来执行上述所有 - 我通常使用的GUI - 但这里是我所知道的每一个步骤:

  1. git remote add his_repo https://github.com/him/his_repo.git
  2. git branch branch_for_his_work his_repo/master,其次是git checkout --merge branch_for_his_work。这将切换到跟踪他的回购的分支,并且--merge意味着Git不会使用他的文件覆盖工作副本的文件,而是开始三向合并。
  3. 要用你的文件覆盖他的文件,解析合并,以便只有你的文件被暂存,而不是他的。然后git commit …
  4. git push …
  5. git remote remove origin,如果你想打电话给他的远程“出身”,而不是“his_repo”也git remote rename his_repo origin。大概也是git branch -M branch_for_his_work master这样“主人”是你唯一的分支,并且指的是你的顾问的历史。

步骤2和3比他们需要,因为我不知道任何标志git checkout,这意味着一个有点困难“离开工作副本,正是因为它是 - 只是改变Git的HEAD”。如果有这样的标志,您可以在步骤3中只需git add -Agit commit,而不必处理合并冲突。或者,您可以在第2步之前将文件复制出来,然后在步骤3之前将它们放回,这会占用更多的磁盘空间,但更简单,就像您所说的使用手动解决方案一样。

+0

感谢您的帮助! – Jessica 2014-10-26 22:24:02

这听起来像你可能会用树枝混乱库:

现在好像我有两个分支,并键入“git的结帐大师”给我我的文件,同时键入“混帐结账tmp_branch”给我他的文件。

如果您的评论是正确的,那么您实际上没有单独的存储库,只是在同一个存储库上单独分支 - 这使得这个超级容易修复。如果你想合并你的分支进入他的,然后继续他的工作,只需做到这一点:

git checkout tmp_branch 
git merge origin master 

这就是它的全部! :-)

但是,这里也值得一提的是,你可能可能不想和他一样在同一个分支上工作。如果你真的在做同样的任务,那可能就是你想要做的事情,但是通常开发人员正在处理单独的任务。举例来说,如果您正在进行用户身份验证,并且您的老板正在为管理员添加一些功能,那么您可能需要他在一个名为“admin”的分支上,并且您可以在名为“user -auth“


对不起,另一注 - 通常,当你的东西与一个开发团队工作,你也从来没有直接对分公司工作。最好在您自己的分支上进行开发,并且只有在您的新代码已经过测试并且正在工作之后,然后将其合并到主分支以将其部署到生产中。只是一个参考:-)。