删除尚未推送的 git commit
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1611215/
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
Remove a git commit which has not been pushed
提问by hap497
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.
我做了一个,git commit
但我还没有将它推送到存储库。因此,当我这样做时git status
,我得到 '#Your 分支领先于 'master' 1 次提交。
So if I want to roll back my top commit, can I just do:
所以如果我想回滚我的最高提交,我可以这样做:
git reset --hard eb27bf26dd18c5a34e0e82b929e0d74cfcaab316
given that when I do git log
I get:
鉴于当我这样做时,git log
我得到:
commit eb27bf26dd18c5a34e0e82b929e0d74cfcaab316 Date: Tue Sep 29 11:21:41 2009 -0700 commit db0c078d5286b837532ff5e276dcf91885df2296 Date: Tue Sep 22 10:31:37 2009 -0700
回答by Brian Campbell
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
,你应该指的是承诺,你是复位到; 所以你可能想要db0c078
提交。
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 HEAD^
,重置为当前头部之前的先前提交;这样你就不必复制提交 ID。
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.
当您执行任何操作时要小心git reset --hard
,因为您可能会丢失任何未提交的更改。您可能需要检查git status
以确保您的工作副本是干净的,或者您确实想要清除那里的任何更改。
In addition, instead of HEAD you can use origin/master
as reference, as suggested by @bdonlan in the comments: git reset --hard origin/master
此外,origin/master
正如@bdonlan 在评论中所建议的那样,您可以将 HEAD用作参考:git reset --hard origin/master
回答by Jeril Kuruvila
IF you have NOT pushed your changes to remote
如果您尚未将更改推送到远程
git reset HEAD~1
Check if the working copy is clean by git status
.
检查工作副本是否干净git status
。
ELSE you have pushed your changes to remote
否则,您已将更改推送到远程
git revert HEAD
This command will revert/remove the last one commit/change and then you can push
此命令将恢复/删除最后一个提交/更改,然后您可以推送
回答by Shadoweb
回答by papigee
There are two branches to this question (Rolling back a commit does not mean I want to lose all my local changes):
这个问题有两个分支(回滚提交并不意味着我想丢失所有本地更改):
1.To revert the latest commit and discard changes in the committed filedo:
1.要恢复最新提交并 放弃已提交文件中的更改,请执行以下操作:
git reset --hard HEAD~1
git reset --hard HEAD~1
2.To revert the latest commit but retain the local changes (on disk)do:
2.要恢复最新提交但保留本地更改(在磁盘上),请执行以下操作:
git reset --soft HEAD~1
git reset --soft HEAD~1
This (the later command) will take you to the state you would have been if you did git add
.
这(后面的命令)将带您进入如果您这样做的状态git add
。
If you want to unstage the files after that, do
如果您想在此之后取消暂存文件,请执行
git reset
git reset
Now you can make more changes before adding and then committing again.
现在,您可以在添加然后再次提交之前进行更多更改。
回答by marcdahan
Simply type in the console :
只需在控制台中输入:
$ git reset HEAD~
This command discards all local commits ahead of the remote HEAD
此命令在远程 HEAD 之前丢弃所有本地提交
回答by M. Massula
I believe that one of those will fit your need
我相信其中之一将满足您的需要
1 - Undo commit and keep all files staged:
git reset --soft HEAD~;
1 - 撤消提交并保持所有文件暂存:
git reset --soft HEAD~;
2 - Undo commit and unstage all files:
git reset HEAD~;
2 - 撤消提交并取消暂存所有文件:
git reset HEAD~;
3 - Undo the commit and completely remove all changes:
git reset --hard HEAD~;
3 - 撤消提交并完全删除所有更改:
git reset --hard HEAD~;
回答by Karina Haddad
Remove the last commit before push
删除推送前的最后一次提交
git reset --soft HEAD~1
git reset --soft HEAD~1
1
means the last commit, if you want to remove two last use 2
, and so forth*
1
表示最后一次提交,如果您想删除两个 last use 2
,依此类推*
回答by Kamlesh Patidar
I have experienced the same situation I did the below as this much easier.
By passing commit-Id
you can reach to the particular commit you want to go:
我经历了与下面相同的情况,因为这更容易。通过传递commit-Id
你可以到达你想要去的特定提交:
git reset --hard {commit-id}
As you want to remove your last commit so you need to pass the commit-Id
where you need to move your pointer:
由于您想删除上次提交,因此您需要传递commit-Id
需要移动指针的位置:
git reset --hard db0c078d5286b837532ff5e276dcf91885df2296
回答by Adnan
This is what I do:
这就是我所做的:
First checkout your branch (for my case master
branch):
首先结帐您的分支(对于我的案例master
分支):
git checkout master
Then reset to remote HEAD^ (it'll remove all your local changes), force clean and pull:
然后重置为远程 HEAD^(它将删除所有本地更改),强制清理并拉取:
git reset HEAD^ --hard && git clean -df && git pull
回答by saga123
One way would be to delete the local branch and checkout that branch from the server if your local branch is ahead of remote by multiple commits and you need to uncommit all of them.
一种方法是删除本地分支并从服务器检出该分支,如果您的本地分支通过多次提交领先于远程并且您需要取消所有提交。