Trying to pull files from my Github repository: “refusing to merge unrelated histories”

前端 未结 8 477
傲寒
傲寒 2020-12-04 05:34

I\'m learning git, and I\'m following the Git community book.

Previously (long time ago) I made a public repository on Github, with some files. Now I set up a local

相关标签:
8条回答
  • 2020-12-04 06:02

    In my case was facing the same issue, especially the first pull request trying after remotely adding a Git repository. The following error was facing.

    fatal: refusing to merge unrelated histories on every try
    

    Use the --allow-unrelated-histories command. It works perfectly.

    git pull origin branchname --allow-unrelated-histories
    
    0 讨论(0)
  • 2020-12-04 06:06
    git checkout master
    git merge origin/master --allow-unrelated-histories
    

    Resolve conflict, then

    git add -A .
    git commit -m "Upload"
    git push
    
    0 讨论(0)
  • 2020-12-04 06:10

    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.

    I find it is slightly less complicated, doesn't require remembering an obscure flag, and keeps the history a bit cleaner.

    0 讨论(0)
  • 2020-12-04 06:15

    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.

    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.

    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."

    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.

    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.

    0 讨论(0)
  • 2020-12-04 06:18

    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:

    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.

    0 讨论(0)
  • 2020-12-04 06:21

    On your branch - say master, pull and allow unrelated histories

    git pull origin master --allow-unrelated-histories
    

    Worked for me.

    0 讨论(0)
提交回复
热议问题