155

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 Git repository on my current computer, and committed some files. Then I added a remote pointing to my Github page:

[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:

[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):

[root@osboxes c]# git status
On branch master
nothing to commit, working directory clean
MichaelSB
  • 3,131
  • 3
  • 26
  • 40
  • 4
    When you set up your local repo, did you clone your Github repo or just did `git init`? In the latter case those repos are unrelated (have no common commits) and you can't merge them (pull is fetch+merge). – Paul Jul 07 '16 at 21:40
  • I did git init. So should I clone my Github repo to fix this? – MichaelSB Jul 07 '16 at 21:41
  • 1
    You can clone your Github repo and continue work with it, but it'll still be separate repo. Do you want to merge two unrelated histories together? – Paul Jul 07 '16 at 21:42
  • I guess I want to merge histories, but really I just want to combine files both locally and on github. I mean I don't really care about history of the old files I have on Github. – MichaelSB Jul 07 '16 at 21:44
  • If you really need local history (from fresh repo you created with `git init`) you may export it as a series of patches and then try to apply them to cloned repo. Otherwise just clone the repo, add missing files and commit them. – Paul Jul 07 '16 at 21:48
  • Great, that worked. However, now I want to update my Github repo with my local files. I did this: `[root@osboxes c]# git push learnc fatal: The current branch master has no upstream branch. To push the current branch and set the remote as upstream, use git push --set-upstream learnc master` What does it mean? – MichaelSB Jul 07 '16 at 22:00
  • 3
    http://stackoverflow.com/a/36528527/2303202 – max630 Jul 10 '16 at 12:58

8 Answers8

321

Try --allow-unrelated-histories

Like max630 commented, or as explained here Git refusing to merge unrelated histories

Community
  • 1
  • 1
Nevermore
  • 7,141
  • 5
  • 42
  • 64
  • 1
    as noted on the [github release notes](https://github.com/git/git/blob/master/Documentation/RelNotes/2.9.0.txt#L58-L68) – systemaddict Sep 12 '16 at 09:44
  • Why is there no config option to alway set this? Every time I have to hunt down this option; I work with git since 2k8 and am totally annoyed by this baby-sitting. Stupid option wasn't always there. At least the refusal message should include the override to use. – nyov Jul 12 '18 at 20:29
103
git checkout master
git merge origin/master --allow-unrelated-histories

Resolve conflict, then

git add -A .
git commit -m "Upload"
git push
Vy Do
  • 46,709
  • 59
  • 215
  • 313
42

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.

BigJMoney
  • 525
  • 4
  • 5
13

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

git pull origin master --allow-unrelated-histories

Worked for me.

Edgar256
  • 702
  • 9
  • 13
  • 1
    This command is the one you need agreeing to add a License or ReadMe when creating an origin repo in Github. – F1Linux Jun 10 '19 at 17:19
12

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.

captncraig
  • 22,118
  • 17
  • 108
  • 151
4

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.

Tasnim Fabiha
  • 1,148
  • 1
  • 17
  • 31
  • 1
    Actual version: `git pull --rebase=merge --allow-unrelated-histories` as `--rebase=preserve` is deprecated https://git-scm.com/docs/git-pull#Documentation/git-pull.txt---rebasefalsetruemergespreserveinteractive – Luckylooke Jun 04 '20 at 12:15
4

Execute the following command:

git pull origin master --allow-unrelated-histories

A merge vim will open. Add some merging message and:

  1. Press ESC
  2. Press Shift + ';'
  3. Press 'w' and then press 'q'.

And you are good to go.

KayV
  • 12,987
  • 11
  • 98
  • 148
0

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
Jitendra Rathor
  • 607
  • 8
  • 11