git 如何删除最后 3 个提交?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40228651/
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 do i delete the last 3 commits?
提问by HattrickNZ
How do i delete the last 3 commits?there is probably many examples but I think i ma being very specific here with what I want.
如何删除最后 3 个提交?可能有很多例子,但我认为我在这里对我想要的东西非常具体。
So for example I have done a git add .
and git commit -m "message"
and a git push -u origin master
for all these.
因此,例如,我已经做了git add .
,并git commit -m "message"
和git push -u origin master
所有这些。
I have the following output from all my commits(with 21 being the most recent commit)
我的所有提交都有以下输出(21 是最近的提交)
$git log --pretty=oneline
[[33mab18ba3884244e99d99122e788062b2aaf6ba372[[m commit-21
[[33m9625c19e03a747457162d7c0274d75b154a0b926[[m commit-20
[[33m8668df12af9eee540cc29f9deb6d31d873bb8f24[[m commit-19
[[33m8315cd7962c902d39160fcf2fd018f249e7cf744[[m commit-18
[[33m3bcecc602a70fb60324777831d51c81b6f9ccaa7[[m commit-17
[[33m0485a39c37e72eabb4003e07a6b8cbae7a0d4e25[[m commit-16
[[33m9d3b04a7bcbb82be8b6b7882511f8133745c93b6[[m commit-15
[[33m22c259497571390fdd1461f0dd6b77244851984b[[m commit-14
[[33m39fb965fc245be55bb86491b5871dd2def08fdf4[[m commit-13
[[33m299f6bb77e922d707c9981a48d4d75f57b724719[[m commit-12
[[33me7285377d7afc08c7a5fb0ca44154d2de7e1d275[[m commit-11
[[33mb3360786ef13044d13b6e58c2239cce5595a1abf[[m commit-10
[[33m9af4c134e0d239d1c34fc6bb6087f0473c187fd5[[m commit-9
[[33md55ac3370f506a4bbf8a4690b9285e5de9c6a671[[m commit-8
[[33me50ef5f58b18d05c36343114804d8be180d26bcb[[m commit-7
[[33m962e4a8de7649e06df29f9058a600f8318caf023[[m commit-6
[[33m37f5363f62a3f973fe6e0d516e47b4324186d998[[m commit-5
[[33m0033e32339e4dc0cce8bd208d43b18a4e9ab43d9[[m commit-4
[[33m345239c740a408826f1df0dc5592d5d6b355f019[[m commit-3
[[33m8c85bf2592f52302ff389a5b6af4127fbe04c73b[[m commit-2
[[33ma4077b6c2b6a491af72ae3afc3b5c6260090b605[[m commit-1
What I want to do is effectively delete the last 3 commits and end up with this:(So it was like git commit 19, 20 and 21 never happened)
我想要做的是有效地删除最后 3 次提交并最终得到这个:(所以就像 git commit 19、20 和 21 从未发生过一样)
$git log --pretty=oneline
[[33m8315cd7962c902d39160fcf2fd018f249e7cf744[[m commit-18
[[33m3bcecc602a70fb60324777831d51c81b6f9ccaa7[[m commit-17
[[33m0485a39c37e72eabb4003e07a6b8cbae7a0d4e25[[m commit-16
[[33m9d3b04a7bcbb82be8b6b7882511f8133745c93b6[[m commit-15
[[33m22c259497571390fdd1461f0dd6b77244851984b[[m commit-14
[[33m39fb965fc245be55bb86491b5871dd2def08fdf4[[m commit-13
[[33m299f6bb77e922d707c9981a48d4d75f57b724719[[m commit-12
[[33me7285377d7afc08c7a5fb0ca44154d2de7e1d275[[m commit-11
[[33mb3360786ef13044d13b6e58c2239cce5595a1abf[[m commit-10
[[33m9af4c134e0d239d1c34fc6bb6087f0473c187fd5[[m commit-9
[[33md55ac3370f506a4bbf8a4690b9285e5de9c6a671[[m commit-8
[[33me50ef5f58b18d05c36343114804d8be180d26bcb[[m commit-7
[[33m962e4a8de7649e06df29f9058a600f8318caf023[[m commit-6
[[33m37f5363f62a3f973fe6e0d516e47b4324186d998[[m commit-5
[[33m0033e32339e4dc0cce8bd208d43b18a4e9ab43d9[[m commit-4
[[33m345239c740a408826f1df0dc5592d5d6b355f019[[m commit-3
[[33m8c85bf2592f52302ff389a5b6af4127fbe04c73b[[m commit-2
[[33ma4077b6c2b6a491af72ae3afc3b5c6260090b605[[m commit-1
Note:I have looked into this and can't quiet get what I want. I have tried git revert(this is advise if I have published which I presume is the same as git push -u origin master
) git rebase
git reset
I am not concerned with my remote at this stage I jsut want to get my local repo put back a couple of repos.
注意:我已经研究过这个并且无法安静地得到我想要的东西。我已经尝试过git revert(如果我已经发布了我认为与它相同的建议,这是建议git push -u origin master
)git rebase
git reset
在这个阶段我不关心我的遥控器我只是想让我的本地 repo 放回几个 repos。
回答by Felipe Sabino
use can use git reset
for that
使用可以git reset
用于那个
git reset --hard HEAD~3
git push --force origin master
where HEAD~3
means go back 3 commits
其中HEAD~3
意味着返回 3 次提交
You can also use the commit number instead of 3 commits, so that you are sure where you are going back in time, like
您还可以使用 commit number 而不是3 commits,这样您就可以确定回到过去的位置,例如
git reset --hard 8315cd7962c902d39160fcf2fd018f249e7cf744
EDIT:
编辑:
When rewriting history, prefer using
改写历史时,更喜欢使用
git push origin +master
To be safe if you are ever pushing more than one branch. You can read more at Git Force push syntax, "-f" versus "+branch"
为了安全,如果您曾经推动多个分支。您可以在Git Force push 语法中阅读更多内容,“-f”与“+branch”
(kudor to Josef Kufnercomment pointing it out)
(对约瑟夫·库夫纳(JosefKufner )评论指出的点赞)
EDIT 2:
编辑2:
I just noticed that your log is printing colors as codes, So
我刚刚注意到您的日志将颜色打印为代码,所以
git reset -- hard 33m8315cd7962c902d39160fcf2fd018f249e7cf744
will not work, [[33m
and [[m
are color codes! So your commit number is actually 8315cd7962c902d39160fcf2fd018f249e7cf744
不会用,[[33m
而且[[m
是颜色代码!所以你的提交号实际上是8315cd7962c902d39160fcf2fd018f249e7cf744
If you do
如果你这样做
git reset --hard 8315cd7962c902d39160fcf2fd018f249e7cf744
it should work as well.
它也应该工作。
(Fixed the commit code on previous examples as well)
(也修复了之前示例中的提交代码)