【问题标题】:check in local workspace into new git repo将本地工作区签入新的 git repo
【发布时间】:2016-07-03 08:09:17
【问题描述】:

我有一个本地工作区,我想保存在 git 中,直到现在这不是本地 git 存储库。只是工作区 我在 git bash 中所做的步骤

cd 我的工作空间

git 初始化

git 添加。

git commit -m "第一次提交"

git远程添加源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 pull origin master

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 repo 创建为 java 的,所以目前它只有 .gitignore 和 README.md

在谷歌搜索时,我发现将现有工作区签入 git 的步骤是

$ cd my_project

$ git 初始化

$ 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 或来源中提取?
  • 打开 git 配置并检查 origin 是否真的是您正在使用的 url...

标签: java eclipse git github


【解决方案1】:

这里的问题是您已经在本地工作空间中初始化存储库,其中已经包含一些文件。当您在 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] 中添加模式:stackoverflow.com/questions/38167476/…
猜你喜欢
  • 1970-01-01
  • 2014-11-01
  • 1970-01-01
  • 2016-10-01
  • 2013-03-08
  • 2014-03-29
  • 2019-08-07
  • 1970-01-01
  • 2011-01-18
相关资源
最近更新 更多