Git 推送失败,“非快进更新被拒绝”
我已经通过 Git Online 编辑了我的 GIT 存储库。在尝试推送本地代码更改后,出现错误:
Git push failed, To prevent from losing history, non-fast forward updates were rejected.
如何解决此问题?
I've edited my GIT repositories via Git Online. After I tried to push my local code changes, I got an error:
Git push failed, To prevent from losing history, non-fast forward updates were rejected.
How can I fix this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
首先拉取更改:
Pull changes first:
如果您确定要推送,请在命令行中添加 --force 。例如,使用
git push origin --force
(我推荐使用命令行,因为您会通过命令行找到其他用户的更多支持。而且,这对于 SmartGit 来说可能是不可能的。)请参阅此站点更多信息:http://help.github.com/remotes/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/在推送之前,使用 rebase 选项执行 git pull。这将获取您在线(在您的源中)所做的更改并在本地应用它们,然后在其之上添加您的本地更改。
现在,您可以推送到远程
有关更多信息,请查看 Git rebase 解释 和 第 3.6 章 Git 分支 - 变基。
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.
Now, you can push to remote
For more information take a look at Git rebase explained and Chapter 3.6 Git Branching - Rebasing.
我遇到了同样的错误,只需在命令中添加 "--force" 即可
I encountered the same error, just add "--force" to the command, it works
解决此问题的最安全方法是使用
--rebase
例如,
这可能会导致本地分支发生冲突,您需要手动修复它们。
解决所有冲突后,您可以使用
--force-with-lease
推送更改,例如
使用此标志,Git 检查分支的远程版本是否与您 rebase 的版本相同,即,如果有人在您变基时推送了新的提交,则该推送将被拒绝,并且您将被迫再次对您的分支进行变基。
如果您正在处理一个每小时有数百次提交的大型项目,这会很烦人,但这仍然是解决此问题的最佳方法。
避免使用--force除非你确切地知道自己在做什么。
使用
--force
具有破坏性,因为它会无条件地用本地的任何内容覆盖远程存储库。但使用
--force-with-lease
可以确保您不会覆盖其他人的工作。请在 Atlassian 的开发者博客中查看这篇文章的更多信息。
The safest way to solve this is using
--rebase
E.g.
This may cause conflicts in your local branch, and you will need to fix them manually.
Once you resolve all the conflicts, you can push your change with
--force-with-lease
E.g.
Using this flag, Git checks if the remote version of the branch is the same as the one you rebase, i.e. if someone pushed a new commit while you were rebasing, the push is rejected, and you will be forced to rebase your branch again.
It is annoying if you are working on a large project with hundreds of commits every hour, but it is still the best way to solve this problem.
AVOID USING --force unless you know exactly what you are doing.
Using
--force
is destructive because it unconditionally overwrites the remote repository with whatever you have locally.But with
--force-with-lease
, ensure you don't overwrite other's work.See more info on this post in Atlassian's developer blog.
我也遇到过同样的问题。
原因是,我的本地分支机构不知何故失去了对远程分支机构的跟踪。
后,我就可以推动了。
解决了合并冲突
I've had the same problem.
The reason was, that my local branch had somehow lost the tracking to the remote counterpart.
After
and resolving the merging conflicts, I was able to push.
使用
--rebase
选项对我有用。然后推送到存储库。
git push <远程>
例如
git pull origin master --rebase
git Push origin master
Using the
--rebase
option worked for me.git pull <remote> <branch> --rebase
Then push to the repo.
git push <remote> <branch>
E.g.
git pull origin master --rebase
git push origin master
(一)Netbeans 7.1的解决方案:尝试拉取。这很可能也会失败。现在查看日志(它们通常显示在 IDE 中)。有一行或多行说:
“由于此文件而拉取失败:”
搜索该文件,将其删除(之前进行备份)。通常它是一个 .gitignore 文件,因此您不会删除代码。重做推送。现在一切都应该正常了。
(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:
"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.
我也遇到过同样的问题。
我解决了
I've hade the same problem.
I resolved with
这对我有用。它可以在 git 文档此处中找到,
如果您在您想要的分支上,您可以执行以下操作:
This is what worked for me. It can be found in git documentation here
If you are on your desired branch you can do this:
遇到了同样的问题,要解决它,请运行以下 git 命令。
git pull {url} --rebase
git push --set-upstream {url} master
您必须先在 github 上创建存储库。
Encountered the same problem, to solve it, run the following
git
commands.git pull {url} --rebase
git push --set-upstream {url} master
You must have created the repository on github first.
有时,当从 git 中拉取时,HEAD 会分离。您可以通过输入命令来检查:
最好移动到您的分支并从各自的分支获取新的拉取
Sometimes, while taking a pull from your git, the HEAD gets detached. You can check this by entering the command:
It's better to move to your branch and take a fresh pull from your respective branch.