【问题标题】:How do I add the contents of a github repository to another github repository?如何将 github 存储库的内容添加到另一个 github 存储库?
【发布时间】:2014-10-26 18:45:47
【问题描述】:

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

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

到目前为止,我尝试做的是

me@server:~/rootdir$ git remote add origin https://github.com/him/his_repo.git
fatal: remote origin already exists.
me@server:~/rootdir$ git remote add his_repo https://github.com/him/his_repo.git
me@server:~/rootdir$ git push -u his_repo master
Username for 'https://github.com': me@gmail.com
Password for 'https://me@gmail.com@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.
me@server:~/rootdir$ git pull his_repo
Username for 'https://github.com': me@gmail.com
Password for 'https://me@gmail.com@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.
me@server:~/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'
me@server:~/rootdir$ git checkout -b origin/master
Switched to a new branch 'origin/master'
me@server:~/rootdir/subdir$ git checkout -b master
fatal: A branch named 'master' already exists.
me@server:~/rootdir/subdir$ git checkout master
Checking out files: 100% (2409/2409), done.
Switched to branch 'master'
me@server:~/rootdir$ git checkout tmp_branch
warning: unable to rmdir mysubdir: Directory not empty
Switched to branch 'tmp_branch'
me@server:~/rootdir$ git mv * tmp_branch/*
fatal: destination 'tmp_branch/*' is not a directory
me@server:~/rootdir$ cd ..
me@server:~/rootdir$ git checkout master
Checking out files: 100% (2409/2409), done.
Switched to branch 'master'
me@server:~/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
me@server:~/rootdir$ git push -u his_repo master
Username for 'https://github.com': me@gmail.com
Password for 'https://me@gmail.com@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.

非常感谢:)

【问题讨论】:

  • “将我的存储库添加到他的存储库”到底是什么意思?您正在寻找的结果是什么?
  • 理想情况下会有一个存储库(他的),我会将所有数据添加到其中。我最终做的是将我的数据复制到一个临时文件夹,删除我的整个存储库,将他的数据拉到一个新文件夹中,然后将我的数据移动到那个新文件夹中。
  • 可能有更好的方法,但我是 n00b。

标签: git github multiple-repositories


【解决方案1】:

让您的顾问的存储库包含您的文件,并忘记您的存储库:

  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。这将切换到一个跟踪他的 repo 的分支,--merge 意味着 Git 不会用他的文件覆盖你的工作副本,而是开始三向合并。
  3. 要用您的文件覆盖他的文件,请解决合并问题,以便只暂存您的文件,而不暂存他的文件。然后git commit …。
  4. git push …
  5. git remote remove origin,如果你想调用他的远程“origin”而不是“his_repo”,还有git remote rename his_repo origin。可能还有git branch -M branch_for_his_work master,因此“master”是您唯一的分支,并且指的是您的顾问的历史记录。

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

【讨论】:

  • 感谢您的帮助!
【解决方案2】:

听起来您可能会将存储库与分支混淆:

现在好像我有两个分支,输入“git checkout master”给我我的文件,而输入“git checkout tmp_branch”给我他的文件。

如果您的评论是正确的,那么您实际上没有单独的存储库,只是在同一个存储库上的不同分支 - 这使得解决这个问题非常容易。如果您想将您的分支合并到他的分支中,然后继续使用他的分支,只需执行以下操作:

git checkout tmp_branch
git merge origin master

这就是它的全部内容! :-)

然而,在这里也值得一提的是,您可能不想和他在同一个分支上工作。如果您实际上是在处理相同的任务,那可能就是您想要做的,但通常开发人员正在处理不同的任务。因此,例如,如果您正在处理用户身份验证并且您的老板正在为管理员添加一些功能,您可能希望他在一个名为 "admin" 的分支上,您可以在一个叫做“user-auth”。


抱歉,另一个注意事项 - 通常,当您与开发团队一起工作时,您也永远不会直接在 ma​​ster 分支上工作。最好在您自己的分支上进行开发,并且只有在您的新代码经过测试并且可以正常工作后,然后将其合并到主分支以将其部署到生产中。仅供参考:-)。

【讨论】:

    猜你喜欢
    • 2019-08-04
    • 1970-01-01
    • 1970-01-01
    • 2021-10-03
    • 2012-10-05
    • 2017-04-06
    • 2011-11-04
    • 2012-09-23
    • 1970-01-01
    相关资源
    最近更新 更多