删除尚未推送的git commit [英] Remove a git commit which has not been pushed

查看:65
本文介绍了删除尚未推送的git commit的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做了一个git commit,但是我还没有将它推送到存储库中. 因此,当我执行git status时,我得到'#您的分支比'master'提前1次提交.

I did a git commit but I have not pushed it to the repository yet. So when I do git status, I get '# Your branch is ahead of 'master' by 1 commit.

因此,如果我想回滚我的最高提交,我可以这样做吗:

So if I want to roll back my top commit, can I just do:

git reset --hard eb27bf26dd18c5a34e0e82b929e0d74cfcaab316

鉴于我做git log会得到:


commit eb27bf26dd18c5a34e0e82b929e0d74cfcaab316
Date:   Tue Sep 29 11:21:41 2009 -0700


commit db0c078d5286b837532ff5e276dcf91885df2296
Date:   Tue Sep 22 10:31:37 2009 -0700

推荐答案

实际上,当您使用git reset时,应该引用要将重置为的提交;因此,您可能需要db0c078提交.

Actually, when you use git reset, you should refer to the commit that you are resetting to; so you would want the db0c078 commit, probably.

一个更简单的版本是git reset --hard HEAD^,可以将其重置为当前磁头之前的上一次提交;这样,您就不必在提交ID周围进行复制.

An easier version would be git reset --hard HEAD^, to reset to the previous commit before the current head; that way you don't have to be copying around commit IDs.

当执行任何git reset --hard时要当心,因为您可能会丢失所有尚未提交的更改.您可能要检查git status以确保您的工作副本是干净的,或者您确实希望删除其中的所有更改.

Beware when you do any git reset --hard, as you can lose any uncommitted changes you have. You might want to check git status to make sure your working copy is clean, or that you do want to blow away any changes that are there.

此外,可以使用origin/master代替HEAD,就像@bdonlan在注释中建议的那样:git reset --hard origin/master

In addition, instead of HEAD you can use origin/master as reference, as suggested by @bdonlan in the comments: git reset --hard origin/master

这篇关于删除尚未推送的git commit的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆