繁体   English   中英

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

[英]How do I add the contents of a github repository to another github repository?

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

非常感谢 :)

要使顾问的存储库包含您的文件,并忘记存储库,请执行以下操作:

  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的分支,并且--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 -Agit commit ,而不必处理合并冲突。 或者,您可以将文件复制到步骤2之前,然后放回步骤3之前,这将使用更多的磁盘空间,但更简单,就像您说的手动解决方案一样。

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

现在看来,我有两个分支,输入“ git checkout master”给我文件,而输入“ git checkout tmp_branch”给我文件。

如果您的评论正确,则实际上您没有单独的存储库,而在同一存储库上只有单独的分支-这使得此超级易于修复。 如果要将分支合并到他的分支中,然后继续使用他的分支,只需执行以下操作:

git checkout tmp_branch
git merge origin master

这里的所有都是它的! :-)

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


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

暂无
暂无

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

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