使用 git reset --hard 后无法推送更改
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9804211/
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
Can not push changes after using git reset --hard
提问by mans
I had a mistake and commit some changes to git which I should not have committed. After I made the commit, I pushed my changes. I then used the following commands to try and reset my changes.
我犯了一个错误并对 git 进行了一些我不应该提交的更改。提交后,我推送了我的更改。然后我使用以下命令来尝试重置我的更改。
git reset --hard head
Now I want to push this 'reset' to the remote repository with this command:
现在我想使用以下命令将此“重置”推送到远程存储库:
git push MyBranch
But I am getting this error:
但我收到此错误:
remote: error: denying non-fast-forward refs/heads/branch (you should pull first)
I tried to use this command without any success:
我尝试使用此命令但没有成功:
git push -f "origin"
Any idea what I can do?
知道我能做什么吗?
回答by VonC
git push -f origin myBranch
should work (provided you are aware this can be dangerous if MyBranch was already fetched by others in their own repo)
应该可以工作(前提是你知道如果 MyBranch 已经被其他人在他们自己的 repo 中获取了,这可能是危险的)
Note: if your remote repo ('origin') has its configset with
注意:如果您的远程仓库('origin')的配置设置为
receive.denyNonFastForwards true
it will deny any non fast-forward push (even when forced).
See "Is there a way to configure git repository to reject 'git push --force'?".
它将拒绝任何非快进推动(即使是被迫的)。
请参阅“有没有办法配置 git 存储库以拒绝 'git push --force'?”。
The OP user654019reports
I managed to solve the problem this time by setting
denyNonFastForwards
tofalse
and using-f
to force the push
这次我设法通过设置
denyNonFastForwards
为false
并使用-f
强制推送来解决问题
If the OP didn't have access to the repo, he/she would have to:
如果 OP 无权访问 repo,他/她将必须:
- reset the local HEAD to its original position (see "Recover from
git reset --hard?
"):git reset HEAD@{1}
- make a newcommit which cancel your merge, as described in the ProGit book, with
git revert
:git revert -m 1 HEAD~
(in your case)
- 将本地 HEAD 重置到其原始位置(请参阅“从恢复
git reset --hard?
”):git reset HEAD@{1}
- 进行一个新的提交,取消您的合并,如ProGit 书中所述,使用
git revert
:(git revert -m 1 HEAD~
在您的情况下)
By example:
举例:
$ git revert -m 1 [sha_of_C8]
Finished one revert.
[master 88edd6d] Revert "Merge branch 'jk/post-checkout'"
1 files changed, 0 insertions(+), 2 deletions(-)
A complete discussion on how to revert a merge can be found here.
可以在此处找到有关如何恢复合并的完整讨论。
The idea remains to generate only newcommits, including one reverting the changes introduced by the merge commit.
You then can push that new commit, as a fast-forward change.
这个想法仍然是只生成新的提交,包括恢复由合并提交引入的更改。
然后,您可以推送该新提交,作为快进更改。
回答by ralphtheninja
You need to specify what ref you want to push:
您需要指定要推送的引用:
git push -f origin MyBranch