Git push 无法工作。

3

你好,我刚开始接触Git,并试图创建本地仓库,并提交了代码,但当我尝试使用推送命令时,它给出了以下错误:


    SUMITs-Mac-mini:gitupload sumit$ git push -u origin master
To https://github.com/sumittiwari87/First-Gulp-Lesson.git
 ! [rejected]        master -> master (non-fast-forward)
error: failed to push some refs to 'https://github.com/sumittiwari87/First-Gulp-Lesson.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.

所以我尝试拉取提交更改,但是我又遇到了错误。
 SUMITs-Mac-mini:gitupload sumit$ git pull origin master
    warning: no common commits
    remote: Counting objects: 4, done.
    remote: Compressing objects: 100% (4/4), done.
    remote: Total 4 (delta 0), reused 0 (delta 0), pack-reused 0
    Unpacking objects: 100% (4/4), done.
    From https://github.com/sumittiwari87/First-Gulp-Lesson
     * branch            master     -> FETCH_HEAD
     * [new branch]      master     -> origin/master
    Auto packing the repository in background for optimum performance.
    See "git help gc" for manual housekeeping.
    fatal: refusing to merge unrelated histories

我不明白为什么会发生这种情况。


如果我必须猜的话,那么我会检查您是否添加了正确的远程URL。 - Jeff Puckett
3个回答

1

fatal: refusing to merge unrelated histories

您的分支没有共同的基础提交历史记录。默认情况下允许此行为,但通常不是预期的结果(不同的提交)。

当您执行合并操作时,您可以使用 --allow-unrelated-histories 选项(默认情况下,拉取操作只是一种获取和合并的方式,可以配置为执行变基操作)。但通常情况下,这不是预期的行为,特别是对于新手来说(除非您已经了解此行为和历史记录)。

我的建议是查看此存储库的本地和远程历史记录:git log --oneline --decorate --all --graph。如果您需要帮助解释输出,请编辑您的问题并显示输出内容。


谢谢。我进行了更多的研究,发现每当我创建一个带有自述文件的新存储库时,它会进行提交,这与我的本地提交不匹配,从而导致冲突。我创建了一个没有自述文件的新存储库,并最终将我的代码推送到GitHub。哇! - s_k_t
@s_k_t 是的,没错。即使是 GitHub 也要遵守将更改提交到仓库的 git 规则,所以这就是添加 readme 所做的事情。很高兴你弄明白了! - Thomas Stringer

0

你尝试过使用git pull解决冲突(如果有的话)然后再进行操作吗?

git add .  
git commit -m "comment"
git push origin master

?


-1

git pull -u origin master。我也可以做。 你没有加上 -u。


这没有任何区别。只是看你是否想设置跟踪。 - scrowler

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