git 试图从我的 Github 存储库中提取文件:“拒绝合并不相关的历史”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38255655/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Trying to pull files from my Github repository: "refusing to merge unrelated histories"
提问by MichaelSB
I'm learning git, and I'm following the Git community book.
我正在学习 git,并且正在关注 Git 社区书籍。
Previously (long time ago) I made a public repository on Github, with some files. Now I set up a local Git repository on my current computer, and committed some files. Then I added a remote pointing to my Github page:
以前(很久以前)我在 Github 上创建了一个公共存储库,其中包含一些文件。现在我在我当前的计算机上设置了一个本地 Git 存储库,并提交了一些文件。然后我添加了一个指向我的 Github 页面的遥控器:
[root@osboxes c]# git remote add learnc https://github.com/michaelklachko/Learning-C
That seemed to be successful:
这似乎是成功的:
[root@osboxes c]# git remote show learnc
* remote learnc
Fetch URL: https://github.com/michaelklachko/Learning-C
Push URL: https://github.com/michaelklachko/Learning-C
HEAD branch: master
Remote branch:
master tracked
Local ref configured for 'git push':
master pushes to master (local out of date)
Now I want to download the files from my Github repo to my computer. I did this:
现在我想将文件从我的 Github 存储库下载到我的计算机。我这样做了:
[root@osboxes c]# git fetch learnc
[root@osboxes c]# git merge learnc/master
warning: refname 'learnc/master' is ambiguous.
Already up-to-date.
However, I don't see any new files in my local directory. How can I get them?
但是,我在本地目录中没有看到任何新文件。我怎样才能得到它们?
I also tried to do this:
我也尝试这样做:
[root@osboxes c]# git pull learnc master
From https://github.com/michaelklachko/Learning-C
* branch master -> FETCH_HEAD
fatal: refusing to merge unrelated histories
BTW, locally I'm on master branch (there are no other branches):
顺便说一句,在本地我在 master 分支上(没有其他分支):
[root@osboxes c]# git status
On branch master
nothing to commit, working directory clean
回答by Nevermore
Try --allow-unrelated-histories
尝试 --allow-unrelated-histories
Like max630 commented, or as explained here Git refusing to merge unrelated histories
就像 max630 评论的那样,或者像这里解释的那样Git 拒绝合并不相关的历史
回答by Do Nhu Vy
git checkout master
git merge origin/master --allow-unrelated-histories
Resolve conflict, then
解决冲突,然后
git add -A .
git commit -m "Upload"
git push
回答by BigJMoney
While I'm all for unblocking people's work issues, I don't think "push --force" or "--allow_unrelated_histories" should be taught to new users as general solutions because they can cause real havoc to a repository when one uses them without understand why things aren't working in the first place.
虽然我完全赞成解除人们的工作问题,但我认为不应将“push --force”或“--allow_unrelated_histories”作为通用解决方案教给新用户,因为当人们使用它们时,它们可能会对存储库造成真正的破坏不明白为什么事情一开始就不起作用。
When you have a situation like this where you started with a local repository, and want to make a remote on GitHub to share your work with, there is something to watch out for.
当您遇到这样的情况时,您从本地存储库开始,并希望在 GitHub 上创建一个远程存储库来共享您的工作,则需要注意一些事项。
When you create the new online repository, there's an option "Initialize this repository with a README". If you read the fine print, it says "Skip this step if you're importing an existing repository."
创建新的在线存储库时,有一个选项“使用 README 初始化此存储库”。如果您阅读细则,它会说“如果您要导入现有存储库,请跳过此步骤。”
You may have checked that box. Or similarly, you made an add/commit online before you attempted an initial push. What happens is you create a unique commit history in each place and they can't be reconciled without the special allowance mentioned in Nevermore's answer (because git doesn't want you to operate that way). You can follow some of the advice mentioned here, or more simply just don't check that option next time you want to link some local files to a brand new remote; keeping the remote clean for that initial push.
您可能已经选中了那个框。或者类似地,您在尝试初始推送之前在线进行了添加/提交。发生的情况是你在每个地方创建了一个独特的提交历史,如果没有 Nevermore 的回答中提到的特殊津贴,它们就无法协调(因为 git 不希望你以这种方式操作)。您可以遵循这里提到的一些建议,或者更简单地说,下次您想将某些本地文件链接到全新的遥控器时,不要选中该选项;保持遥控器清洁以进行初始推送。
Reference: my first experience with git + hub was to run into this same problem and do a lot of learning to understand what had happened and why.
参考:我第一次使用 git + hub 时遇到了同样的问题,并做了很多学习以了解发生了什么以及为什么。
回答by captncraig
If there is not substantial history on one end (aka if it is just a single readme commit on the github end), I often find it easier to manually copy the readme to my local repo and do a git push -f
to make my version the new root commit.
如果一端没有实质性的历史记录(也就是如果它只是 github 端的单个自述文件提交),我通常会发现手动将自述文件复制到我的本地存储库并执行一个git push -f
使我的版本成为新的根提交更容易.
I find it is slightly less complicated, doesn't require remembering an obscure flag, and keeps the history a bit cleaner.
我发现它稍微不那么复杂,不需要记住一个不起眼的标志,并使历史记录更清晰。
回答by Edgar256
On your branch - say master, pull and allow unrelated histories
在你的分支上 - 说 master,pull 并允许不相关的历史
git pull origin master --allow-unrelated-histories
Worked for me.
对我来说有效。
回答by KayV
Execute the following command:
执行以下命令:
git pull origin master --allow-unrelated-histories
A merge vim will open. Add some merging message and:
一个合并 vim 将打开。添加一些合并消息并:
- Press ESC
- Press Shift + ';'
- Press 'w' and then press 'q'.
- 按ESC
- 按 Shift + ';'
- 按“w”然后按“q”。
And you are good to go.
你很高兴去。
回答by Tasnim Fabiha
When I used --allow-unrelated-histories
, this command generated too many conflicts. There were conflicts in files which I didn't even work on. To get over the error " Refusing to merge unrelated histories"
, I used following rebase command:
当我使用时--allow-unrelated-histories
,这个命令产生了太多的冲突。我什至没有处理过的文件中存在冲突。为了克服错误" Refusing to merge unrelated histories"
,我使用了以下 rebase 命令:
git pull --rebase=preserve --allow-unrelated-histories
After this commit the uncommitted changes with a commit message. Finally, run the following command:
在此提交未提交的更改和提交消息之后。最后,运行以下命令:
git rebase --continue
After this, my working copy was up-to-date with the remote copy and I was able to push my changes as before. No more unrelated histories error while pulling.
在此之后,我的工作副本与远程副本保持同步,我能够像以前一样推送我的更改。拉动时不再出现无关的历史错误。