删除未推送的 git 提交
我进行了 git commit ,但尚未将其推送到存储库。
因此,当我执行 git status
时,我得到“# Yourbranch is before 'master' by 1 commit”。
因此,如果我想回滚我的最高提交,我可以这样做吗:
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(13)
如果您尚未将更改推送到远程
通过
git status
检查工作副本是否干净。否则您已将更改推送到远程
此命令将恢复/删除本地提交/更改,然后您可以推送
IF you have NOT pushed your changes to remote
Check if the working copy is clean by
git status
.ELSE you have pushed your changes to remote
This command will revert/remove the local commits/change and then you can push
实际上,当你使用
git reset
时,你应该引用你要重置的提交;到;所以您可能需要 db0c078 提交。一个更简单的版本是 git reset --hard HEAD^ ,重置到当前 head 之前的上一次提交;这样您就不必复制提交 ID。
当您执行任何 git reset --hard 操作时要小心,因为您可能会丢失任何未提交的更改。您可能需要检查 git status 以确保您的工作副本是干净的,或者您确实想要清除其中的任何更改。
此外,您可以使用
origin/master
作为参考,而不是 HEAD,正如 @bdonlan 在评论中建议的那样:git reset --hard origin/master
Actually, when you use
git reset
, you should refer to the commit that you are resetting to; so you would want thedb0c078
commit, probably.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.Beware when you do any
git reset --hard
, as you can lose any uncommitted changes you have. You might want to checkgit status
to make sure your working copy is clean, or that you do want to blow away any changes that are there.In addition, instead of HEAD you can use
origin/master
as reference, as suggested by @bdonlan in the comments:git reset --hard origin/master
我相信其中之一将满足您的需求
1 - 撤消提交并保留所有文件:
git reset --soft HEAD~
2 - 撤消提交并取消暂存所有文件:
git reset HEAD~
3 - 撤消提交并完全删除所有更改:
git reset --hard HEAD~
这是我找到答案的地方
I believe that one of those will fit your need
1 - Undo commit and keep all files staged:
git reset --soft HEAD~
2 - Undo commit and unstage all files:
git reset HEAD~
3 - Undo the commit and completely remove all changes:
git reset --hard HEAD~
here is were I found the answer
它适用于其他分支:
将其重置为原点所在的位置。
这是 @bdonlan 在评论中发布的。我为那些不看评论的人添加了这个答案。
It works for other branch:
to reset it to whatever the origin was at.
This was posted by @bdonlan in the comments. I added this answer for people who don't read comments.
这个问题有两个分支(回滚提交并不意味着我想丢失所有本地更改):
1.恢复最新提交并放弃已提交文件中的更改< /strong> do:
git reset --hard HEAD~1
2. 恢复最新提交,但保留本地更改(在磁盘上) do:
git reset --soft HEAD~1
这条命令(后面的命令)将把你带到你执行 git add 时的状态。
如果您想在此之后取消暂存文件,请执行
git reset
现在,您可以在添加然后再次提交之前进行更多更改。
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 file do:
git reset --hard HEAD~1
2. To revert the latest commit but retain the local changes (on disk) do:
git reset --soft HEAD~1
This (the later command) will take you to the state you would have been if you did
git add
.If you want to unstage the files after that, do
git reset
Now you can make more changes before adding and then committing again.
删除推送前的最后一次提交
git reset --soft HEAD~1
1
表示最后一次提交,如果要删除最后两个使用 <代码>2,依此类推*Remove the last commit before push
git reset --soft HEAD~1
1
means the last commit, if you want to remove two last use2
, and so forth*只需在控制台中输入:
此命令会丢弃远程 HEAD 之前的所有本地提交
Simply type in the console :
This command discards all local commits ahead of the remote HEAD
我经历过与下面相同的情况,因为这更容易。
通过传递
commit-Id
,您可以到达您想要去的特定提交:由于您想删除最后一次提交,因此您需要传递
commit-Id
需要移动指针: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:As you want to remove your last commit so you need to pass the
commit-Id
where you need to move your pointer:这就是我所做的:
首先签出您的分支(对于我的情况
master
分支):然后重置为远程 HEAD^ (它将删除所有本地更改),强制清洁和拉动:
This is what I do:
First checkout your branch (for my case
master
branch):Then reset to remote HEAD^ (it'll remove all your local changes), force clean and pull:
一种方法是删除本地分支并从服务器检出该分支,如果您的本地分支领先于远程多次提交,并且您需要取消所有提交。
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.
我刚刚遇到了同样的问题,最终做了:
(N 是 git 将向您显示的提交数量)
这会提示您的文本编辑器,然后您可以通过删除与其关联的行来删除您想要的提交。
I just had the same problem and ended up doing:
(N is the number of commits git will show you)
That prompts your text editor and then you can remove the commit you want by deleting the line associated with it.
从提交中删除文件夹
从提交中删除文件
To delete folder from commit
To delete file from commit
如果其他人正在寻找相反的情况,即恢复到您最初认为应该删除的提交,请使用
git reflog
信用:Murtuzaali Surti 关于git reflog
的文章,用于查找丢失的提交。幸运的是,即使在尝试了多次疯狂的
git reset --soft/hard
命令之后,我也没有设法删除我需要的提交,老实说,我越来越有信心 git<无论如何,/strong> 都会支持我,至少在我做出承诺的情况下。您仍然需要
git reset --hard
但请参阅该文章的链接以获取指导。
If anyone else is looking for the opposite i.e., reverting back to the commit you initially thought you should delete, use
git reflog
credit: Murtuzaali Surti's article ongit reflog
to find the missing commits.I luckily didn't manage to delete the commit I needed back, even after attempting multiple frantic
git reset --soft/hard
commands and honestly I'm becoming more and more confident that git has my back no matter what, at least if a commit has been made.You will still need to
git reset --hard <SHA-found-with-reflog>
But see the link to the article for guidance.