无法推送到 git 存储库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4549828/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 04:52:20  来源:igfitidea点击:

Cannot push to git repository

git

提问by Serhat Ozgel

I keep getting this error message from git while pushing, even I am trying it after pulling over and over again:

我在推送时不断从 git 收到此错误消息,即使我一遍又一遍地尝试它:

 ! [rejected]        master -> master (non-fast-forward)
error: failed to push some refs to '[repo url]'
To prevent you from losing history, non-fast-forward updates were rejected
Merge the remote changes (e.g. 'git pull') before pushing again.  See the
'Note about fast-forwards' section of 'git push --help' for details.

Here is what the branch history looks like:

这是分支历史记录的样子:

A---B---C
  |   |
  D   E-F

A initial commit (me)
B some commit (me)
C master
D some commit (other dev)
E remotes/origin/master - Merge branch 'master' of [repo url]
F Local uncommitted changes, not checked into an index

From here, when I pull, nothing comes. When I push, I get the error. How can I successfully push again?

从这里,当我拉时,什么也没有。当我按下时,我收到错误。我怎样才能再次成功推送?

回答by Michal ?iha?

You need to have common ancestor before pushing, the easiest way is to do git pull(or git pull --rebaseif you want to avoid merge commit and rebase is not an issue for you).

在推送之前你需要有共同的祖先,最简单的方法是做git pull(或者git pull --rebase如果你想避免合并提交和变基对你来说不是问题)。

回答by Kaikass

I had this exact problem, it was due to the following:

我遇到了这个确切的问题,这是由于以下原因:

A local branch in my machine (with different origin) that was not updated (and had been modified by another user). You must move to the branch that has not been updated (git checkout [branch in question]) and do a git pull.

我机器中的本地分支(具有不同的来源)未更新(并且已被另一个用户修改)。您必须移动到尚未更新的分支(git checkout [有问题的分支])并执行 git pull。

That solved my problem. In this particular case, due to this message

那解决了我的问题。在这种特殊情况下,由于此消息

"! [rejected] master -> master (non-fast-forward)!"

“![拒绝] master -> master(非快进)!”

You must move to the master branch (git checkout master) do a git pull, then move back to whatever branch you are working on (git checkout whateverBranch) and now you should be able to do a push without problems.

您必须移动到主分支 (git checkout master) 执行 git pull,然后移回您正在处理的任何分支 (git checkoutwhatBranch),现在您应该能够毫无问题地进行推送。

I'm not positive on why this happens; it may be that the nature of git push is to check all local branches that are not properly updated when doing a push.

我对为什么会发生这种情况并不乐观。可能是 git push 的性质是在执行推送时检查所有未正确更新的本地分支。

回答by Steve Johnson

The solution to your problem is:

您的问题的解决方案是:

  1. "Commit" the changes so that these are committed to your local repository.
  2. Then "Pull" the code from github/git server
  3. Then "Push" the code to github/git server and you will not face any issue.
  1. “提交”更改,以便将这些更改提交到您的本地存储库。
  2. 然后从 github/git 服务器“拉取”代码
  3. 然后将代码“推送”到 github/git 服务器,您将不会遇到任何问题。

Read more under heading "Pushing a Branch" in the following url:

在以下网址中的“推送分支”标题下阅读更多信息:

Dealing with "non-fast forward rejects"

处理“非快进拒绝”

Hope it helps.

希望能帮助到你。

Thanks

谢谢