6
votes

I have installed Github desktop and git on windows machine, I got a github account and created a dummy repository.

When I intend to upload my package through git bash command line, it fails with error: fatal: refusing to merge unrelated histories

I used several ways to overcome this issues by using existing solution from this community, but still didn't fix the problem. Does anyone know any trick of working this problem? How can I upload my projects to github successfully?

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
git pull origin master --allow-unrelated-histories .See here stackoverflow.com/questions/37937984/…Bhargav Variya

3 Answers

4
votes

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 path to your repo directory(e.g. C:\Users\jvrat\Documents\MSPC), and <remote repo URL> with URL to your remote repo (e.g. https://github.com/username/repo_name.git).

13
votes

Just sharing that rebasing worked for me. I had a new project on GitHub, and a new repo 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 (1st is GitHub repo, 2nd is local):

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

PS 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 Handy...

1
votes

The first step is to init 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 repo via

git add <file-name>

Files added to your repo 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 resides on your local git repo. To connect your local repo to a remote one you have to issue another command:

git remote add origin <remote repo URL>

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

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

which is in your case

git push origin master

because for now you just have a master branch.

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

git status