从 Git 的分支中删除提交 [英] Delete commits from a branch in Git

查看:81
本文介绍了从 Git 的分支中删除提交的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何删除提交.

I would like to know how to delete a commit.

通过delete,我的意思是就好像我没有进行那个提交,并且当我将来进行推送时,我的更改不会推送到远程分支.

By delete, I mean it is as if I didn't make that commit, and when I do a push in the future, my changes will not push to the remote branch.

我阅读了 git help,我认为我应该使用的命令是 git reset --hard HEAD.这是正确的吗?

I read git help, and I think the command I should use is git reset --hard HEAD. Is this correct?

推荐答案

注意: git reset --hard 将删除您对工作目录的更改.在运行此命令之前,请务必隐藏您想要保留的任何本地更改.

Careful: git reset --hard WILL DELETE YOUR WORKING DIRECTORY CHANGES. Be sure to stash any local changes you want to keep before running this command.

假设您正在执行该提交,那么此命令将对其进行操作...

Assuming you are sitting on that commit, then this command will wack it...

git reset --hard HEAD~1

HEAD~1 表示 head 之前的提交.

The HEAD~1 means the commit before head.

或者,您可以查看 git log 的输出,找到您要备份的提交的提交 ID,然后执行以下操作:

Or, you could look at the output of git log, find the commit id of the commit you want to back up to, and then do this:

git reset --hard <sha1-commit-id>


如果你已经推送了它,你需要做一个强制推送来摆脱它...


If you already pushed it, you will need to do a force push to get rid of it...

git push origin HEAD --force

然而,如果其他人可能已经拉了它,那么你最好开始一个新的分支.因为当他们拉动时,它只会将其合并到他们的作品中,而您又会将其推回原处.

However, if others may have pulled it, then you would be better off starting a new branch. Because when they pull, it will just merge it into their work, and you will get it pushed back up again.

如果您已经推送,最好使用 git revert,创建一个镜像"提交来撤消更改.但是,两次提交都会在日志中.

If you already pushed, it may be better to use git revert, to create a "mirror image" commit that will undo the changes. However, both commits will be in the log.

仅供参考 -- git reset --hard HEAD 如果你想摆脱 WORK IN PROGRESS 是很棒的.它会将您重置回最近的提交,并清除工作树和索引中的所有更改.

FYI -- git reset --hard HEAD is great if you want to get rid of WORK IN PROGRESS. It will reset you back to the most recent commit, and erase all the changes in your working tree and index.

最后,如果您需要查找您删除"的提交,它通常会出现在 git reflog 中,除非您对存储库进行了垃圾回收.

Lastly, if you need to find a commit that you "deleted", it is typically present in git reflog unless you have garbage collected your repository.

这篇关于从 Git 的分支中删除提交的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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