简体   繁体   English

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

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

Yesterday I created a github repository and today I found out my advisor also created a github repository. 昨天我创建了一个github存储库,今天我发现我的顾问还创建了一个github存储库。 What is the best way to add my repository to his repository without massively screwing things up? 什么是将我的存储库添加到他的存储库而又不花费太多精力的最佳方法是什么? I have massively screwed things up in the past so I just want to make sure. 过去,我已经把事情搞砸了,所以我只想确定一下。 Also I may have already screwed things up by trying to follow the instructions on another StackOverflow post. 另外,我可能已经尝试按照另一个StackOverflow帖子中的说明搞砸了。

Right now it seems like I have two branches, and typing "git checkout master" gives me my files, while typing "git checkout tmp_branch" gives me his files. 现在看来,我有两个分支,输入“ git checkout master”给我文件,而输入“ git checkout tmp_branch”给我文件。 I'd like to add all my files to his repository and use that one from now on, and maybe delete my repository. 我想将所有文件添加到他的存储库中,并从现在开始使用该文件,也许删除我的存储库。

So far what I've tried to do is 到目前为止,我尝试做的是

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.

Thanks so much :) 非常感谢 :)

To make your advisor's repository contain your files, and to forget about your repository: 要使顾问的存储库包含您的文件,并忘记存储库,请执行以下操作:

  1. add his repository as another remote 将他的存储库添加为另一个远程服务器
  2. check out a new local branch that tracks his master branch 签出跟踪其主分支的新本地分支
  3. make a new commit on that branch that replaces his files with yours 在该分支上进行新提交,用您的文件替换他的文件
  4. push that branch to his remote 将该分支推到他的遥控器
  5. remove your old remote from your local repository (and delete it on the remote server if you want) 从本地存储库中删除旧的远程服务器(并根据需要在远程服务器上将其删除)

In your log of shell commands, you already got steps 1 and 4 working. 在shell命令的日志中,您已经执行了步骤1和4。 Step 4 failed because you didn't do steps 2 and 3. When you tried to push your commit history to his remote in step 4, you were on the branch of your old repository, which had completely different history. 第4步失败,因为您没有执行第2步和第3步。当您尝试在第4步中将提交历史记录推送到他的远程数据库时,您所在的旧存储库中的分支历史完全不同。 Git refused to do it because you would be overwriting all of his history with your history. Git拒绝这样做,因为您将用您的历史覆盖他的所有历史。 To fix that, start with his history by checking out his branch, and then add a new commit to the history that replaces his files with yours. 要解决此问题,请先检查他的分支,从他的历史记录开始,然后向历史记录中添加新的提交,以用您的文件替换他的文件。

I haven't memorized the exact git commands to do all of the above – I usually use a GUI – but here is what I know about each step: 我还没有记住执行上述所有操作的确切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 , followed by git checkout --merge branch_for_his_work . git branch branch_for_his_work his_repo/master ,然后是git checkout --merge branch_for_his_work This will switch to a branch that tracks his repo, and --merge means that instead of overwriting the files your working copy with his files, Git will start a three-way merge. 这将切换到一个跟踪他的--merge的分支,并且--merge意味着Git将开始三向合并,而不是用他的文件覆盖您的工作副本中的文件。
  3. To overwrite his files with yours, resolve the merge so that only your files are staged, and none of his. 要用您的文件覆盖他的文件,请解决合并问题,以便仅暂存您的文件,而不保留任何文件。 Then git commit … . 然后git commit …
  4. git push …
  5. git remote remove origin , and also git remote rename his_repo origin if you want to call his remote “origin” instead of “his_repo”. git remote remove origin ,并且如果要调用他的远程“ origin”而不是“ his_repo”,则git remote rename his_repo origin Probably also git branch -M branch_for_his_work master so that “master” is your only branch, and refers to your advisor's history. 可能也是git branch -M branch_for_his_work master因此“ master”是您唯一的分支,它引用顾问的历史记录。

Steps 2 and 3 are a bit harder than they need to be because I don't know of any flag for git checkout that means “leave the working copy exactly as it is – just change Git's HEAD ”. 第2步和第3步比他们需要的要难一些,因为我不知道git checkout的任何标志,这意味着“完全保留工作副本–只需更改Git的HEAD ”。 If there were such a flag, you could just git add -A and git commit in step 3, without having to deal with merge conflicts. 如果存在这样的标志,则只需在步骤3中git add -Agit commit ,而不必处理合并冲突。 Or you could copy your files out before steps 2 and put them back before step 3, which is uses more disk space, but is simpler, much like the manual solution you said you used. 或者,您可以将文件复制到步骤2之前,然后放回步骤3之前,这将使用更多的磁盘空间,但更简单,就像您说的手动解决方案一样。

It sounds like you might be confusing repositories with branches: 听起来您可能会将存储库与分支混淆了:

Right now it seems like I have two branches, and typing "git checkout master" gives me my files, while typing "git checkout tmp_branch" gives me his files. 现在看来,我有两个分支,输入“ git checkout master”给我文件,而输入“ git checkout tmp_branch”给我文件。

If your comment is correct, you actually don't have separate repositories, just separate branches on the same repository - which makes this super easy to fix. 如果您的评论正确,则实际上您没有单独的存储库,而在同一存储库上只有单独的分支-这使得此超级易于修复。 If you want to merge your branch into his and then continue working off of his, simply do this: 如果要将分支合并到他的分支中,然后继续使用他的分支,只需执行以下操作:

git checkout tmp_branch
git merge origin master

That's all there is to it! 这里的所有都是它的! :-) :-)

However, it's also worth mentioning here that you may not want to be working on the same branch as his. 但是,在这里也值得一提的是,您可能不想与他在同一分支上工作。 If you are literally working on the same exact task, that might be what you want to do, but generally developers are working on separate tasks. 如果您实际上是在完成相同的任务,那可能就是您想要做的,但是通常开发人员正在从事单独的任务。 So for example, if you were working on user authentication and your boss was working on adding some functionality for admins, you'd probably want him on a branch called something like "admin" and you could be on one called "user-auth" . 因此,例如,如果您正在进行用户身份验证,而老板正在为管理员添加一些功能,则可能希望他在一个名为“ admin”的分支上,而您可能在一个名为“ user-auth”的分支上。


Sorry, another note - generally when you're working on something with a team of developers, you'll also never work directly on the master branch. 抱歉,另一个注意事项-通常,当您与开发人员团队一起工作时,也永远不会直接在master分支上工作。 It's best to do your development on your own branch, and only after your new code has been tested and is working, then merge it into the master branch to deploy it to production. 最好在自己的分支上进行开发,并且只有在新代码经过测试并可以正常工作之后,再将其合并到master分支中,才能将其部署到生产环境中。 Just an FYI :-) . 仅供参考:-)。

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

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