首次Git推送被拒绝

7
当我输入git push origin master时,Git会显示以下错误信息:
To https://github.com/farrasdoko/RumahSakit.git
 ! [rejected]        master -> master (non-fast-forward)
error: failed to push some refs to 'https://github.com/farrasdoko/RumahSakit.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 fetch origingit merge origin mastergit pull origin master,Git 显示以下错误信息:
From https://github.com/farrasdoko/RumahSakit
 * branch            master     -> FETCH_HEAD
fatal: refusing to merge unrelated histories

如何推送它?
2个回答

4
提示:更新被拒绝,因为您当前分支的提示落后于origin/master。 通常当它发生时,您真正想做的是在您自己的提交之前将origin/master中的额外提交添加到您的分支中。您可以使用rebase命令来实现这一点。
git rebase origin/master

这将从origin拉取新的提交到你的分支中,然后在其之上添加你自己的新提交,以便使你的提交是最新的。 重定位过程中可能会遇到一些冲突。 当出现这种情况时,git会停止并告诉你解决冲突; 在此之后,你应该git add受影响的文件,然后git rebase --continue。 或者,如果你决定不知道如何处理问题,可以git rebase --abort,这样你的分支就会回到git rebase命令开始之前的状态。

git rebase是一个强大的命令,可以快速地搞乱你的分支,所以要小心使用它,但不要害怕它。 标准的计算机建议适用:如果你不自信,在做任何事情之前备份你的工作。 幸运的是,很容易只需创建一个新的分支作为备份 (git checkout -b my_backup_branch),然后切换回你的工作分支。


3
两种方法。我假设你已经检出到主分支。
1.
git merge origin/master --allow-unrelated-histories

解决冲突,然后

git add -A .
git commit -m "Commit message"
git push origin master

或者
2.

git pull -f origin master     //The -f flag stands for force

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