Git 推送失败,“非快进更新被拒绝”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6897600/
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-10 11:27:55  来源:igfitidea点击:

Git push failed, "Non-fast forward updates were rejected"

gitgithubpush

提问by Sarath

I've edited my GIT repositories via Git Online. After I tried to push my local code changes, I got an error:

我已经通过 Git Online 编辑了我的 GIT 存储库。在我尝试推送本地代码更改后,出现错误:

Git push failed, To prevent from losing history, non-fast forward updates were rejected.

How can I fix this?

我怎样才能解决这个问题?

回答by iafonov

Pull changes first:

首先拉取更改:

git pull origin branch_name

回答by Matt

Add --force to your command line if you are sure you want to push. E.g. use git push origin --force(I recommend the command line as you will find much more support from other users with the command line. Also this may not be possible with SmartGit.) See this site for more information: http://help.github.com/remotes/

如果您确定要推送,请将 --force 添加到您的命令行。例如使用git push origin --force(我推荐使用命令行,因为您会发现其他用户使用命令行提供更多支持。此外,SmartGit 可能无法实现。)请参阅此站点以获取更多信息:http: //help.github.com/遥控器/

回答by satishgoda

Before pushing, do a git pull with rebase option. This will get the changes that you made online (in your origin) and apply them locally, then add your local changes on top of it.

在推送之前,使用 rebase 选项执行 git pull。这将获取您在线(在您的源中)所做的更改并在本地应用它们,然后在其上添加您的本地更改。

git pull --rebase

Now, you can push to remote

现在,您可以推送到远程

git push 

For more information take a look at Git rebase explainedand Chapter 3.6 Git Branching - Rebasing.

有关更多信息,请查看Git rebase 解释第 3.6 章 Git 分支 - Rebase

回答by Wen Qi

I encountered the same error, just add "--force"to the command, it works

我遇到了同样的错误,只需在命令中添加“--force”就可以了

git push origin master --force

回答by hardmooth

I've had the same problem.
The reason was, that my local branch had somehow lost the tracking to the remote counterpart.

我遇到了同样的问题。
原因是,我的本地分支不知何故丢失了对远程对应方的跟踪。

After

git branch branch_name --set-upstream-to=origin/branch_name
git pull

and resolving the merging conflicts, I was able to push.

并解决合并冲突,我能够推动。

回答by igorjosesantos

You can add --force-with-leaseto the command, it will works.

您可以将--force-with-lease添加到命令中,它将起作用。

git push --force-with-lease

--forceis destructive because it unconditionally overwrites the remote repository with whatever you have locally. But --force-with-leaseensure you don't overwrite other's work.

--force是破坏性的,因为它无条件地用您本地拥有的任何内容覆盖远程存储库。但是--force-with-lease确保您不会覆盖其他人的工作。

See more info here.

在此处查看更多信息。

回答by CyberDemic

Using the --rebaseoption worked for me.

使用该--rebase选项对我有用。

  • git pull <remote> <branch> --rebase
  • git pull <remote> <branch> --rebase

Then push to the repo.

然后推送到repo。

  • git push <remote> <branch>
  • git push <remote> <branch>

E.g.

例如

git pull origin master --rebase

git pull origin master --rebase

git push origin master

git push origin master

回答by Sliq

(One) Solution for Netbeans 7.1: Try a pull. This will probably also fail. Now have a look into the logs (they are usually shown now in the IDE). There's one/more line saying:

(一)Netbeans 7.1的解决方案:试试pull。这也可能会失败。现在查看日志(它们现在通常显示在 IDE 中)。有一行/多行说:

"Pull failed due to this file:"

“由于此文件,拉取失败:”

Search that file, delete it (make a backup before). Usually it's a .gitignore file, so you will not delete code. Redo the push. Everything should work fine now.

搜索该文件,将其删除(之前进行备份)。通常它是一个 .gitignore 文件,所以你不会删除代码。重做推送。现在一切正常。

回答by CodeChops

This is what worked for me. It can be found in git documentation here

这对我有用。它可以在此处的git 文档中找到

If you are on your desired branch you can do this:

如果你在你想要的分支上,你可以这样做:

git fetch origin
# Fetches updates made to an online repository
git merge origin YOUR_BRANCH_NAME
# Merges updates made online with your local work

回答by blackFoxCoder

Encountered the same problem, to solve it, run the following gitcommands.

遇到同样的问题,要解决,运行以下git命令。

  • git pull {url} --rebase
  • git push --set-upstream {url} master
  • git pull {url} --rebase
  • git push --set-upstream {url} master

You must have created the repository on github first.

您必须先在 github 上创建存储库。