git 将项目上传到github时如何解决“拒绝合并无关历史”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40498753/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 12:24:53  来源:igfitidea点击:

How to fix "refusing to merge unrelated histories" when uploading project to github?

gitgithubgit-bash

提问by Jerry Hamilton

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

我已经在 Windows 机器上安装了 Github 桌面和 git,我有一个 github 帐户并创建了一个虚拟存储库。

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

当我打算通过 git bash 命令行上传我的包时,它失败并显示错误: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?

通过使用该社区的现有解决方案,我使用了多种方法来解决此问题,但仍然没有解决问题。有谁知道解决这个问题的任何技巧?如何成功上传我的项目到github?

采纳答案by TeWu

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/repowith 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).

请务必/path/to/my/repo使用您的存储库目录的路径(例如C:\Users\jvrat\Documents\MSPC)和<remote repo URL>远程存储库的 URL (例如)替换https://github.com/username/repo_name.git

回答by AmitaiB

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:

只是分享重新定位对我有用。我在 GitHub 上有一个新项目,还有一个我想链接到的本地新存储库,并不断获得fatal: refusing to merge unrelated histories. 什么工作:

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 logoutput (1st is GitHub repo, 2nd is local):

git log输出(第一个是 GitHub 存储库,第二个是本地):

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

PS有趣的,我从来没有注意到默认初始提交消息从GitHub是nitial Çommit,而本地一个是nitial Çommit(小写)。我想我会把这个问题发给 Hyman Handy ......

回答by Torsten Barthel

The first step is to init git inside your local project directory:

第一步是在本地项目目录中初始化 git:

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 存储库并包含一个 .git 目录。然后,您基本上创建文件并将其添加到您的存储库中

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 存储库中。要将本地存储库连接到远程存储库,您必须发出另一个命令:

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

'origin' 和以下 URL 代表远程名称及其 URL。您现在可以通过以下方式将本地更改推送到您的原始存储库

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