简体   繁体   English

删除中间的GIT提交

[英]Remove an intermediate GIT commit

I have an intermediate git commit which i have merged into the code now. 我有一个中间的git commit,现在我已将其合并到代码中。 It happened by mistake. 它是错误地发生的。 I have 2 options: 我有2个选择:

  1. Delete the project from gerrit, for which i have to install the delete-project plug in 从gerrit删除项目,为此我必须安装delete-project插件
  2. Delete the commit from the GIT repository. 从GIT存储库中删除提交。

I would like to have suggestions on using the second option as i do not have the admin access to install the plugin. 我想对使用第二个选项提出建议,因为我没有安装插件的管理员权限。

commit 1 提交1

commit 2 提交2

I would like to keep the commit 1 and delete the commit 2. The commit 2 is a direcotry which has many number of files in it. 我想保留提交1并删除提交2。提交2是目录,其中包含许多文件。

Any suggestions ? 有什么建议么 ?

如果您想保留最新的提交,最好使用git rebase: http : //git-scm.com/docs/git-rebase

What about a normal revert? 正常还原怎么办? You want to purge it because of its size? 您要清除它的大小吗?

Then you need the Force Push access right [EDIT: in Gerrit], which enables you to do a 然后,您需要“ Force Push访问权限[编辑:Gerrit中],这使您可以执行

git push --force

after you skipped back that one commit with 在您跳过该提交之后

git reset --hard HEAD^

Be aware that rewriting history is to be avoided when working on a repository with a team of developers. 请注意,与一组开发人员一起使用存储库时,应避免重写历史记录。

I Think you are looking for 我想你在找

$ git reset --hard REF

Here REF will be SHA1 of commit 1 . 此处REF将是commit 1 SHA1

Okay, so you want to get rid of one commit. 好的,所以您想摆脱一次提交。

If you haven't already pushed out the commit, you could drop it locally with 如果尚未推出该提交,则可以使用以下命令将其本地删除

git reset --hard HEAD~

which checks out the revision right before the current. 它会在当前版本之前签出修订。
Your history before: commit1--commit2 您之前的历史记录: commit1--commit2
Your history after: commit1 您的历史记录: commit1

If you already pushed the commit, as long as there's no critical in commit2 (huge, passwords, etc), it's better to keep commit2 in the history and revert the commit instead of dropping it: 如果您已经推送了提交,只要commit2没有关键commit2 (巨大,密码等),最好将commit2保留在历史记录中并还原提交而不是将其删除:

git revert <sha-of-commit2>

This will create a NEW commit that reverts the changes made by commit2 . 这将创建一个NEW提交,该提交将还原commit2所做的commit2
Your history before: commit1--commit2 您之前的历史记录: commit1--commit2
Your history after: commit1--commit2--commit3 之后的历史记录: commit1--commit2--commit3

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM