10

I have installed GitHub Desktop and Git on a Windows machine. I got a GitHub account and created a dummy repository.

When I intend to upload my package through the Git Bash command line, it fails with an error:

fatal: refusing to merge unrelated histories

I used several ways to overcome these issues by using an existing solution from this community, but still it didn't fix the problem. Is there a trick of working this problem? How can I upload my projects to GitHub successfully?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Hamilton
  • 620
  • 2
  • 14
  • 32
  • 3
    All these answers assume you don't want anything from the initial github repo, but github can add files for you, like a default, nicely formatted .gitignore and README. The only way I could find to get around this is an extra `git merge --allow-unrelated-histories`. I wish there were a way to do this in a single `git pull`. – trysis Apr 24 '18 at 19:40
  • 3
    `git pull origin master --allow-unrelated-histories` .See here https://stackoverflow.com/questions/37937984/git-refusing-to-merge-unrelated-histories-on-rebase – Bhargav Variya Mar 20 '21 at 06:48
  • 2
    Does this answer your question? [Git refusing to merge unrelated histories on rebase](https://stackoverflow.com/questions/37937984/git-refusing-to-merge-unrelated-histories-on-rebase) – Michael Freidgeim Jul 06 '22 at 22:51

3 Answers3

17

I am just sharing that rebasing worked for me. I had a new project on GitHub, and a new repository locally that I wanted to link up and kept getting fatal: refusing to merge unrelated histories. What worked:

git remote add origin http://github.com/MyName/MyProjectName -f
git branch -u origin/master
git pull -r     # R for rebase, makes the magic happen

Output:

First, rewinding head to replay your work on top of it...
Applying: Initial Commit

git log output (the first is the GitHub repository, and the second is local):

c7f843e Initial Commit (AmitaiB, 4 minutes ago)
97100be Initial commit (Amitai Blickstein, 9 minutes ago)

PS: It is funny; I never noticed that the default initial commit message from GitHub is Initial Commit, whereas the local one is Initial commit (lowercase). I think I'll send that question in to Jack Handey...

AmitaiB
  • 1,656
  • 1
  • 19
  • 19
5

Try the following:

cd /path/to/my/repo
git init
git add --all
git commit -m "Initial commit"
git remote add origin <remote repo URL>
git push -u origin master

Be sure to replace /path/to/my/repo with the path to your repository directory (e.g., C:\Users\jvrat\Documents\MSPC), and <remote repository URL> with the URL to your remote repository (e.g., https://github.com/username/repo_name.git).

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
TeWu
  • 5,928
  • 2
  • 22
  • 36
  • can't finish rm -r .git command yet. Plus, git add all gave me an error. Any further solution ? – Hamilton Nov 09 '16 at 01:09
  • Does the error message say anything more? You can try [this solution](http://stackoverflow.com/questions/24114676/git-error-failed-to-push-some-refs-to) – TeWu Nov 09 '16 at 01:38
  • your solution is quite helpful, the last comment is useful. Thank you :) – Hamilton Nov 09 '16 at 01:50
  • Plus, where can I find useful git command when I am interacting github if any new commit is taken place ? Any useful git command you could recommend ? Thanks your big help :) – Hamilton Nov 09 '16 at 01:57
  • Just google ["git tutorial"](https://www.google.pl/search?q=git+tutorial), and you'll find a lot of them. Github has [its own interactive git tutorial](https://try.github.io) too. For reference purposes it's best to use [official git docs](https://git-scm.com/docs). – TeWu Nov 09 '16 at 02:18
1

The first step is to initialise Git inside your local project directory:

git init

After that, your directory is a local Git repository and contains a .git directory.

You then basically create files and add it to your repository via

git add <file-name>

Files added to your repository are tracked now. If you want to commit all the changes you made to the files you added, you just need to

git commit "Commit message"

These all reside in your local Git repository.

To connect your local repository to a remote one you have to issue another command:

git remote add origin <remote repo URL>

'origin' and the following URL represent the remote name and its URL. You are now able to push your local changes to your origin repository via

git push <remote-name> <branch-name>

which is in your case

git push origin master

because for now you just have the master branch.

You can check the status of your local repository and its connected remote repository via

git status
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Torsten Barthel
  • 3,059
  • 1
  • 26
  • 22