7

I have a local git repository with already commited code. Now I want to import the repository to github preserving its commits. What I did so far:

I've created a new repository on github.

I've added that as a remote in my local repository:

git remote add origin https://github.com/...

and

git branch --set-upstream-to=origin/master master

But when I try to git pull I get this error:

fatal: refusing to merge unrelated histories

I've heard of the switch to --allow-unrelated-histories, but there was a warning this could cause complications to the repository. I didn't try it.

This questions seems to address the same problem, but the answers are very old and do not address the problem I ran into.

Apart from that I found guides that either assume I have another public repository whos URL I could paste to some import dialog; or they assume I have no version control at all, just a bunch of files.

user1785730
  • 3,150
  • 4
  • 27
  • 50
  • 1
    What code and commits do you have in your local repo? What is in your GitHub repo? If either is just empty or trivial files like README or LICENSE, then you should just delete it and start over creating a new empty repo without any history. – Code-Apprentice Jan 09 '20 at 22:37
  • The local repo contains code. The one on github contains some autogenerated files: .gitignore, LICENSE, README.md. – user1785730 Jan 09 '20 at 22:40

1 Answers1

11

It sounds like you created a GitHub repository with a README file or LICENSE file. GitHub offers to create these for you, but in your case you shouldn't. I suggest that you delete your GitHub repository and create a new completely empty one. Then go through the same procedure that you already did:

git remote add origin https://github.com/...
git branch --set-upstream-to=origin/master master

Instead of git pull, though, you should do git push to upload your current master branch to GitHub.

On the other hand, if your local repository contains no code, you should delete it and run the following command:

git clone https://github.com/...

This will create a new subdirectory in the directory where you run the command. That subdirectory contains a new repo with the remote correctly configured to point at the GitHub repo. It will also contain remote tracking branches for all of the branches from the GitHub repo and automatically checkout master.

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268