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

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

问题描述



通过删除,我的意思是说它是如何删除一个提交的。就好像我没有做出这样的提交,并且在将来我进行推送时,我的更改不会推送到远程分支。



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

/ code> 将删除您的工作目录更改。在运行此命令之前,请确保隐藏您要保留的任何本地更改



假设您正在参与该提交,那么此命令将怪胎......

  git reset --hard HEAD〜1 

HEAD〜1 表示头部之前的提交。



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

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






如果您已经推过它,您需要...

  git push origin HEAD --force 

code>

然而,如果其他人可能拉过它,那么开始一个新分支会更好。因为当他们拉时,它会将它合并到他们的工作中,并且你会再次推回它。



如果您已经推送过,那么最好使用它 git revert ,创建一个镜像镜像提交,以撤消更改。但是,这两个提交都将在日志中。






供参考 - git reset - 如果你想摆脱正在进行的工作,硬头是很棒的。它会将您重置为最近的提交,并清除工作树和索引中的所有更改。






最后,如果你需要找到一个你删除的提交,它通常出现在 git reflog 中,除非你有垃圾收集你的仓库。


I would like to know how to delete a commit.

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.

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

解决方案

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

The HEAD~1 means the commit before head.

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.

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.


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.


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天全站免登陆