将本地工作区提交到新的 Git 仓库中。

3
我有一个本地工作区,希望将其保存在git中,直到现在它还不是本地git仓库。以下是我在git bash中的步骤:
cd myworkspace git init
git add .
git commit -m "第一次提交"
git remote add origin https://github.com/username/eclipse.git git push origin master 这样做时出现了错误。
! [rejected]        master -> master (non-fast-forward)
error: failed to push some refs to 'https://github.com/username/eclipse.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

接着我执行了以下操作:

git拉取远程代码至本地主分支

warning: no common commits
remote: Counting objects: 4, done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 4 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (4/4), done.
From https://github.com/koushikpaul1/eclipse
 * branch            master     -> FETCH_HEAD
 * [new branch]      master     -> origin/master
fatal: refusing to merge unrelated histories

请问有人能帮我解决这个错误么:

注意:我创建的git仓库是一个java仓库,因此它目前只包含.gitignore和README.md文件。

在搜索时,我发现将现有的工作空间存入git的步骤如下:

$ cd my_project

$ git init

$ git add *

$ git commit -m "第一次提交"

$ git remote add origin https://github.com/yourname/my_project.git

$ git pull origin master

$ git push origin master

但是我卡在了倒数第二步

非常感谢!


你尝试从URL或者origin拉取了吗? - Andrew Li
打开 git 配置并检查 origin 是否实际上使用了你正在使用的 URL... - ΦXocę 웃 Пepeúpa ツ
1个回答

2
问题在于你已经在本地工作区初始化了仓库,其中已包含一些文件。当你在GitHub上创建并使用.gitignore和README.md初始化仓库时,它也会包含一些文件。
当你推送项目时,Git将拒绝,因为它不知道如何合并你的内容。
以下是解决方案:
  1. 使用git push -f将所有本地仓库推送到远程仓库,但这也将替换.gitignore和README.md文件,然后你可以稍后手动添加它们。
  2. 删除你的远程Git仓库并重新初始化为裸仓库(不要勾选使用.gitignore、README.md初始化),然后再尝试推送。
  3. 只需将远程仓库克隆到本地工作区,然后手动将你的文件添加到克隆的仓库中。
更多信息请访问: https://help.github.com/articles/creating-a-new-repository/ 参考第5点:

有许多可选项可用于预填充您的存储库。 如果您正在将现有存储库导入GitHub,请不要选择任何这些选项,因为可能会引入合并冲突。 您可以选择稍后从命令行添加这些文件。

您可以创建一个README文件,它是描述您的项目的文档。 您可以创建一个.gitignore文件,其中包含一组忽略规则。

您可以选择为您的项目添加软件许可证。


在您指出原因后,问题变得很容易解决,非常感谢。像魔法一般奏效。我还需要另外一个帮助,当我使用gitbash时,gitignore可以完美地工作,但是在使用eclipse git插件时,即使我在[eclipse]中添加了模式,它也无效:http://stackoverflow.com/questions/38167476/gitignore-not-working-with-eclipse - user3123372

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接