git 如何完成还原提交,以及如何还原大量提交
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20604167/
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
how to finish reverting a commit, and how to revert a lot of commits
提问by Pat Farrell
This has to be simple, but I can't find it in git-scm
.
这必须很简单,但我在git-scm
.
I've been making a lot of small commits to a public project, and all of my work is bad. I want to remove everything that I've done. Some I've just committed locally, some I've pushed to 'origin master'.
我一直在为一个公共项目做出很多小的承诺,但我所有的工作都很糟糕。我想删除我所做的一切。有些我刚刚在本地提交,有些我已经推到了“起源大师”。
The first commit (a week ago) is bdbad86
... with the most recent being e82401b
...
第一次提交(一周前)是bdbad86
......最近一次是e82401b
......
I want to just make all these go away. I've tried to revert one.
我只想让所有这些消失。我试图恢复一个。
git status
# On branch master
# You are currently reverting commit e82401b.
# (all conflicts fixed: run "git revert --continue")
# (use "git revert --abort" to cancel the revert operation)
- I can't figure out how to finish this reverting.
- I don't want to have to do each commit separately, I want to blow them all away.
- 我不知道如何完成这个恢复。
- 我不想每次都分别提交,我想把它们都吹走。
采纳答案by Chris Hayes
I'm assuming here that no one else interjected commits in between your work, and that you bad commits form a continuous range in the repo's history. Otherwise you're going to have to get more complicated. Let's assume your history looks like this:
我在这里假设没有其他人在您的工作之间插入提交,并且您的错误提交在 repo 的历史记录中形成一个连续的范围。否则你将不得不变得更加复杂。假设您的历史记录如下所示:
e82401b - (master, HEAD) My most recent private commit
...
bc2da37 - My first private commit
cf3a183 - (origin/master) My most recent bad public commit
...
292acf1 - My first bad public commit
82edb2a - The last good public commit
The first thing we want to do is blow away the commits that you haven't made public yet. You can do this with the following command (note that your changes will be gone and should be considered unrecoverable):
我们要做的第一件事就是删除您尚未公开的提交。您可以使用以下命令执行此操作(请注意,您的更改将消失并且应被视为不可恢复):
git reset --hard cf3a183
Equivalently (and more readable):
等效地(并且更具可读性):
git reset--hard origin/master
Now your view of the repository agrees with the view in origin/master
. You now want to revert your bad public changes and publish them as a revert commit. These instructions are for creating a single revert commit.
现在,您对存储库的看法与 中的看法一致origin/master
。您现在想要恢复错误的公共更改并将它们作为恢复提交发布。这些说明用于创建单个还原提交。
You can use git revert --no-commit a..b
to revert all the commits starting at the commit aftera
(note that!) and ending at, and including, commit b
. The reversion will be staged for you to commit. So, here, we would do:
您可以使用git revert --no-commit a..b
从提交之后a
(请注意!)开始并以并包括 commit 结束的所有提交b
。将上演还原以供您提交。所以,在这里,我们会这样做:
git revert --no-commit 82edb2a..HEAD
Or, equivalently:
或者,等效地:
git revert --no-commit 292acf1^..HEAD
Remembering that HEAD
now points to the same place as origin/master
.
记住HEAD
now 指向与 相同的位置origin/master
。
After running the revert
command you now have your changes staged and ready to commit, so just run a simple git commit -m "Reverting those bad changes I accidentally pushed and made public"
.
运行该revert
命令后,您现在可以暂存更改并准备提交,因此只需运行一个简单的git commit -m "Reverting those bad changes I accidentally pushed and made public"
.
回答by Michael
Instead of running
而不是跑步
git revert --continue
git revert --continue
Just run
赶紧跑
git commit -m "commit message here"
git commit -m "commit message here"
If that doesn't work, make sure you've staged all the files involved in the revert. Even the files that you reverted and then discarded changes to.
如果这不起作用,请确保您已暂存还原中涉及的所有文件。甚至您还原然后丢弃的文件也会更改。
回答by Nitin Jain
You can set your repo on any previous commit and all changes after that will be removed.
您可以在任何以前的提交上设置您的 repo,之后的所有更改都将被删除。
git reset --hard
git reset --hard
after that to push it to github use git push --force origin
之后将其推送到 github 使用 git push --force origin
visit How do you roll back (reset) a Git repository to a particular commit?