我无法推动 git?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3598355/
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
I am not able to push on git?
提问by user12345
git push origin master
shows an error
git push origin master
显示错误
failed to push some refs to '
[email protected]:xyz/abc.git'
To prevent you from losing history, non-fast-forward updates were rejected Merge the remote changes before pushing again. See the 'Note about fast-forwards' section of 'git push --help
' for details.
未能将一些引用推送到 '
[email protected]:xyz/abc.git'
为防止您丢失历史记录,非快进更新被拒绝 再次推送之前合并远程更改。有关详细信息,请参阅“ ”的“关于快进的注意事项”部分git push --help
。
What is this? How to recover this?
这是什么?如何恢复这个?
回答by VonC
See the "pushing a branch" section from GitHub help page:
请参阅GitHub 帮助页面中的“推送分支”部分:
Dealing with “non-fast-forward” errors
处理“非快进”错误
From time to time you may encounter this error while pushing:
推送时,您有时可能会遇到此错误:
$ git push origin master
To ../remote/
! [rejected] master -> master (non-fast forward)
error: failed to push some refs to '../remote/'
To prevent you from losing history, non-fast-forward updates were rejected
Merge the remote changes before pushing again. See the 'non-fast forward'
section of 'git push --help' for details.
This error can be a bit overwhelming at first, do not fear.
Simply put, git cannot make the change on the remote without losing commits, so it refuses the push. Usually this is caused by another user pushing to the same branch.
You can remedy this by fetching and merging the remote branch, or using pull to perform both at once.In other cases this error is a result of destructive changes made locally by using commands like
git commit --amend
orgit rebase
.
While you can override the remote by adding--force
to the push command, you should only do so if you are absolutely certain this is what you want to do. Force-pushes can cause issues for other users that have fetched the remote branch, and is considered bad practice. When in doubt, don't force-push.
这个错误一开始可能有点让人不知所措,不要害怕。
简而言之,git 无法在不丢失提交的情况下对远程进行更改,因此它拒绝推送。通常这是由另一个用户推送到同一分支引起的。
您可以通过获取和合并远程分支或使用 pull 同时执行这两项来解决此问题。在其他情况下,此错误是使用
git commit --amend
或 之类的命令在本地进行破坏性更改的结果git rebase
。
虽然您可以通过添加--force
到 push 命令来覆盖远程,但只有在您绝对确定这是您想要执行的操作时才应该这样做。强制推送可能会给已经获取远程分支的其他用户带来问题,被认为是不好的做法。如有疑问,请不要强行推动。
Mode details on the "Note about fast-forwards" of git push
, as mentioned by Michael Mrozekin the comments.
的“关于快进的注意事项”的git push
模式详细信息,正如Michael Mrozek在评论中所提到的。
回答by Joe Lewis
In addition to VonC's answer,
除了 VonC 的回答,
In case you, intend to override the remote changes with your local,
如果您打算用本地覆盖远程更改,
$git push --force
will do.
会做。
回答by eMPee584
Quite important: this can also occur if name of the branch you are on (and want to merge) and the branch upstream are not the same. Then work flow could look like this:
非常重要:如果您所在的分支(并想要合并)和上游分支的名称不同,也会发生这种情况。然后工作流程可能如下所示:
git pull --rebase # to retrieve upstream changes, replay yours on top
git push --dry-run --verbose origin HEAD:master # always good idea: dry-runs..
git push origin HEAD:master # actually push current branch head (non-master) to upstream master
回答by jclaan
If you are getting this error and your .git/config file is just fine then go through these steps (this is a common problem with a submodule in git repos - again do this from the submodule dir)
如果您收到此错误并且您的 .git/config 文件很好,那么请执行这些步骤(这是 git repos 中子模块的常见问题 - 再次从子模块目录执行此操作)
- git checkout master
- git pull
- (merge happens in background)
- git push
- git结帐大师
- 拉
- (合并发生在后台)
- 推
回答by Arjun
First Pull then push
先拉后推
git pull origin any_branch_name
git pull origin any_branch_name
回答by Steve Chambers
I had this too and couldn't find anywhere on StackOverflow with the following useful nugget clearly stated: Git won't let you push to a different branch if your working branch has diverged from it. There's an easy fix - just switch to the branch you want to push to and merge your working branch into it. So instead of trying this:
我也有这个,但在 StackOverflow 上找不到任何明确说明以下有用信息的地方:如果您的工作分支已经偏离,Git 不会让您推送到不同的分支。有一个简单的修复方法 - 只需切换到您想要推送到的分支并将您的工作分支合并到其中即可。所以不要尝试这个:
git push origin master <========== On "mybranch"
Do this:
做这个:
git checkout master <========== Switch to the branch you want to push to
git pull origin master <========== Get latest from remote repository
git pull origin mybranch <========== Merge in changes from "mybranch"
======== Resolve any issues ========
git push origin master <========== Push the merged changes