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

时间:2014-10-26 18:45:47

标签: git github multiple-repositories

昨天我创建了一个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.

非常感谢:)

2 个答案:

答案 0 :(得分:1)

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

  1. 将他的存储库添加为另一个远程
  2. 查看跟踪其主分支的新本地分支
  3. 在该分支上进行新的提交,用您的文件替换他的文件
  4. 将该分支推送到他的远程
  5. 从本地存储库中删除旧的远程(如果需要,可以在远程服务器上将其删除)
  6. 在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,还有git remote rename his_repo origin,如果你想调用他的远程“起源”而不是“his_repo”。也可能是git branch -M branch_for_his_work master所以“主人”是你唯一的分支,并且是指顾问的历史。
    6. 步骤2和3比他们需要的更难,因为我不知道git checkout的任何标志意味着“完全保留工作副本 - 只需改变Git的{{1} }”。如果有这样的标志,您可以在步骤3中HEADgit add -A,而不必处理合并冲突。或者你可以在第2步之前复制你的文件,然后在步骤3之前将它们放回去,这会占用更多的磁盘空间,但更简单,就像你说过的手动解决方案一样。

答案 1 :(得分:1)

听起来你可能会混淆带分支的存储库:

  

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

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

git checkout tmp_branch
git merge origin master

这就是它的全部! : - )

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


抱歉,另一个注意事项 - 通常当您与开发人员团队合作时,您也永远不会直接在分支上工作。最好在您自己的分支上进行开发,并且只有在您的新代码经过测试并且正在运行之后,才能将其合并到主分支中以将其部署到生产中。只是一个FYI :-)。

相关问题