简体   繁体   English

如何将本地 git 存储库导入到 github?

[英]How to import local git repository to github?

I have a local git repository with already commited code.我有一个已经提交代码的本地 git 存储库。 Now I want to import the repository to github preserving its commits.现在我想将存储库导入 github 并保留其提交。 What I did so far:到目前为止我做了什么:

I've created a new repository on github.我在 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:但是当我尝试git pull我得到这个错误:

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.我听说切换到--allow-unrelated-histories ,但是有一个警告,这可能会导致存储库的复杂性。 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;除此之外,我发现指南假设我有另一个公共存储库,我可以将其 URL 粘贴到某个导入对话框; 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.听起来您创建了一个带有 README 文件或 LICENSE 文件的 GitHub 存储库。 GitHub offers to create these for you, but in your case you shouldn't. GitHub 提供为您创建这些,但在您的情况下,您不应该这样做。 I suggest that you delete your GitHub repository and create a new completely empty one.我建议您删除您的 GitHub 存储库并创建一个新的完全空的存储库。 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.但是,您应该执行git push而不是git pull将您当前的主分支上传到 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.该子目录包含一个新的存储库,其中远程正确配置为指向 GitHub 存储库。 It will also contain remote tracking branches for all of the branches from the GitHub repo and automatically checkout master .它还将包含来自 GitHub 存储库的所有分支的远程跟踪分支,并自动检出master

I'm trying to do the same from the local git repository that I just created by migrating a local svn repository.我正在尝试从我刚刚通过迁移本地 svn 存储库创建的本地 git 存储库执行相同的操作。 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: git remote add origin https://github.com/...执行成功但第二条命令失败:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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