撤消“git push”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1270514/
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
Undoing a 'git push'
提问by Cyrus
Here's what I did on my supposed-to-be-stablebranch...
这是我在我应该稳定的分支上所做的......
% git rebase master
First, rewinding head to replay your work on top of it...
Fast-forwarded alpha-0.3.0 to master.
% git status
# On branch alpha-0.3.0
# Your branch is ahead of 'origin/alpha-0.3.0' by 53 commits.
#
nothing to commit (working directory clean)
% git push
Fetching remote heads...
refs/
refs/heads/
refs/tags/
refs/remotes/
'refs/heads/master': up-to-date
updating 'refs/heads/alpha-0.3.0'
from cc4b63bebb6e6dd04407f8788938244b78c50285
to 83c9191dea88d146400853af5eb7555f252001b0
done
'refs/heads/unstable': up-to-date
Updating remote server info
That was all a mistake as I later realized. I'd like to undo this entire process, and revert the alpha-0.3.0 branch back to what it was.
正如我后来意识到的那样,这完全是一个错误。我想撤消整个过程,并将 alpha-0.3.0 分支恢复到原来的样子。
What should I do?
我该怎么办?
回答by CB Bailey
You need to make sure that no other users of this repository are fetching the incorrect changes or trying to build on top of the commits that you want removed because you are about to rewind history.
您需要确保此存储库的其他用户没有获取不正确的更改或尝试在要删除的提交之上构建,因为您将要回滚历史记录。
Then you need to 'force' push the old reference.
然后您需要“强制”推送旧参考。
git push -f origin last_known_good_commit:branch_name
or in your case
或者在你的情况下
git push -f origin cc4b63bebb6:alpha-0.3.0
You may have receive.denyNonFastForwards
set on the remote repository. If this is the case, then you will get an error which includes the phrase [remote rejected]
.
您可能已经receive.denyNonFastForwards
在远程存储库上进行了设置。如果是这种情况,那么您将收到一个错误,其中包含短语[remote rejected]
。
In this scenario, you will have to delete and recreate the branch.
在这种情况下,您必须删除并重新创建分支。
git push origin :alpha-0.3.0
git push origin cc4b63bebb6:refs/heads/alpha-0.3.0
If this doesn't work - perhaps because you have receive.denyDeletes
set, then you have to have direct access to the repository. In the remote repository, you then have to do something like the following plumbing command.
如果这不起作用 - 可能是因为您已经receive.denyDeletes
设置,那么您必须直接访问存储库。在远程存储库中,您必须执行类似于以下管道命令的操作。
git update-ref refs/heads/alpha-0.3.0 cc4b63bebb6 83c9191dea8
回答by Benny Wong
I believe that you can also do this:
我相信你也可以这样做:
git checkout alpha-0.3.0
git reset --hard cc4b63bebb6
git push origin +alpha-0.3.0
This is very similar to the last method, except you don't have to muck around in the remote repo.
这与最后一种方法非常相似,只是您不必在远程存储库中四处游荡。
回答by neoneye
git revert
is less dangerous than some of the approaches suggested here:
git revert
比这里建议的一些方法危险性小:
prompt> git revert 35f6af6f77f116ef922e3d75bc80a4a466f92650
[master 71738a9] Revert "Issue #482 - Fixed bug."
4 files changed, 30 insertions(+), 42 deletions(-)
prompt> git status
# On branch master
# Your branch is ahead of 'origin/master' by 1 commit.
#
nothing to commit (working directory clean)
prompt>
Replace 35f6af6f77f116ef922e3d75bc80a4a466f92650 with your own commit.
将 35f6af6f77f116ef922e3d75bc80a4a466f92650 替换为您自己的提交。
回答by Saboosh
The accepted solution (from @charles bailey) is highly dangerous if you are working in a shared repo.
如果您在共享存储库中工作,则已接受的解决方案(来自 @charles Bailey)是非常危险的。
As a best practice, all commits pushed to a remote repo that is shared should be considered 'immutable'. Use 'git revert' instead: http://www.kernel.org/pub/software/scm/git/docs/user-manual.html#fixing-mistakes
作为最佳实践,推送到共享的远程存储库的所有提交都应被视为“不可变的”。改用“git revert”:http: //www.kernel.org/pub/software/scm/git/docs/user-manual.html#fixing-mistakes
回答by curmil
A way to do it without losing the changes you wanted:
一种在不丢失所需更改的情况下执行此操作的方法:
git reset cc4b63b
git stash
git push -f origin alpha-0.3.0
git stash pop
Then you can choose the files you meant to push
然后你可以选择你想要推送的文件
回答by Rushabh Mehta
Another way to do this:
另一种方法来做到这一点:
- create another branch
- checkout the previous commit on that branch using "git checkout"
- push the new branch.
- delete the old branch & push the delete (use
git push origin --delete <branch_name>
) - rename the new branch into the old branch
- push again.
- 创建另一个分支
- 使用“git checkout”检查该分支上的先前提交
- 推新分支。
- 删除旧分支并推送删除(使用
git push origin --delete <branch_name>
) - 将新分支重命名为旧分支
- 再推。
回答by ireshika piyumalie
git push origin +7f6d03:master
This will revert your repo to mentioned commit number
这会将您的回购恢复到提到的提交编号
回答by Jaymin
Undo multiple commits git reset --hard 0ad5a7a6 (Just provide commit SHA1 hash)
撤消多次提交 git reset --hard 0ad5a7a6 (只需提供提交 SHA1 哈希值)
Undo last commit
撤消上次提交
git reset --hard HEAD~1 (changes to last commit will be removed ) git reset --soft HEAD~1 (changes to last commit will be available as uncommited local modifications)
git reset --hard HEAD~1(对上次提交的更改将被删除) git reset --soft HEAD~1(对上次提交的更改将作为未提交的本地修改可用)
回答by Barani r
Scenario 1: If you want to undo the last commit say 8123b7e04b3, below is the command(this worked for me):
场景 1:如果你想撤销最后一次提交,比如 8123b7e04b3,下面是命令(这对我有用):
git push origin +8123b7e04b3^:<branch_name>
Output looks like below:
输出如下所示:
Total 0 (delta 0), reused 0 (delta 0)
To https://testlocation/code.git
+ 8123b7e...92bc500 8123b7e04b3^ -> master (forced update)
Additional info:Scenario 2: In some situation, you may want to revert back what you just undo'ed (basically undo the undo) through the previous command, then use the below command:
附加信息:场景 2:在某些情况下,您可能希望通过上一个命令恢复您刚刚撤消的内容(基本上撤消撤消),然后使用以下命令:
git reset --hard 8123b7e04b3
Output:
输出:
HEAD is now at cc6206c Comment_that_was_entered_for_commit
More info here: https://github.com/blog/2019-how-to-undo-almost-anything-with-git
更多信息:https: //github.com/blog/2019-how-to-undo-almost-anything-with-git
回答by Vlad274
The existing answers are good and correct, however what if you need to undo the push
but:
现有的答案是好的和正确的,但是如果您需要撤消但是怎么办push
:
- You want to keep the commits locally or you want to keep uncommitted changes
- You don't know how many commits you just pushed
- 您想在本地保留提交或您想保留未提交的更改
- 你不知道你刚刚推送了多少次提交
Use this command to revert the change to the ref:
使用此命令恢复对 ref 的更改:
git push -f origin refs/remotes/origin/<branch>@{1}:<branch>