简体   繁体   中英

How to import local git repository to github?

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.

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 .

I'm trying to do the same from the local git repository that I just created by migrating a local svn repository. It has only a master branch:

thanos@win10-ubuntu:~/down/svn-git/home-automation.git
$ git branch -a
* master

The git remote add origin https://github.com/... is executed successfully but the second command fails:

thanos@win10-ubuntu:~/down/svn-git/home-automation.git
$ git branch --set-upstream-to=origin/master master
error: the requested upstream branch 'origin/master' does not exist

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM