将本地repo与github repo合并导致致命错误

时间:2018-05-18 18:38:23

标签: git github

我正在使用git版本2.16.2。我在本地有一个repo并在github上创建了一个新的repo来获取我已在本地创建的文件。我做了:

git init
git add origin https://github.com/me/repo

然后(我提交后)我去推:

git push origin master

我得到了:

$ git push -u origin master
To https://github.com/me/repo
 ! [rejected]        master -> master (non-fast-forward)
error: failed to push some refs to 'https://github.com/me/repo'
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.

I found this documentation所以我跑了:

$ git pull

给出了:

From https://github.com/me/repo
 * [new branch]      master     -> origin/master
There is no tracking information for the current branch.
Please specify which branch you want to merge with.
See git-pull(1) for details.

    git pull <remote> <branch>

If you wish to set tracking information for this branch you can do so with:

    git branch --set-upstream-to=origin/<branch> master

所以我再去推,我得到了:

$ git push -u origin master
To https://github.com/r-wells/react-fun
 ! [rejected]        master -> master (non-fast-forward)
error: failed to push some refs to 'https://github.com/me/repo'
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.

然后我意识到我没有拉master所以我跑:

$ git pull https://github.com/me/repo master

但我明白了:

From https://github.com/r-wells/react-fun
 * branch            master     -> FETCH_HEAD
fatal: refusing to merge unrelated histories

我认为$ git pull会整合远程更改。我假设我错过了一些小事。有人看到我没看到的东西吗?

1 个答案:

答案 0 :(得分:1)

错误告诉您本地分支落后于主人。

Updates were rejected because the tip of your current branch is behind

1)尝试git pull origin master --rebase

如果它不起作用,那么本地和远程分支,不同步。 您必须先将本地主分支与远程主分支同步。

请按照以下步骤操作:

  1. 复制您最近提交的新提交哈希。
  2. 运行git fetch origin
  3. 运行git reset --hard origin/master#来同步你的 当地主人与远程主人
  4. cherry-pick your commit:git cherry-pick your_commit_hash
  5. git push origin master#推送你的提交 如果仍未解决,请告诉我。
  6. 谢谢!