从git历史记录中删除并完全删除提交 [英] Delete and completely remove the commit from git history

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

问题描述

我的git历史记录中有一个提交

I have a commits in my git history

1.commit 4930da17d8dd23d650ed38435d8b421816a0c451
  Date:   Sat Dec 5 14:34:18 2015 +0530

2.commit e1ebbbb599ee20ebec3ca92c26266d9fd16e7ccc
  Date:   Sat Dec 5 13:22:20 2015 +0530

3.commit 1c4a11a80eb054d24dafec2efed0b0282188e687
  Date:   Sat Dec 5 12:11:50 2015 +0530

4.commit b4ab3c164a3a8d93e0a71a94b5c561cb5e20ebf6
  Date:   Sat Dec 5 12:09:56 2015 +0530

5.commit 167b1d10258381f09663ce61fa88ce3bbcd404c4
  Date:   Sat Dec 5 12:09:21 2015 +0530

6.commit c61bcadac673e1c83f4c14b66d56e23b12fa3198
  Date:   Sat Dec 5 12:07:58 2015 +0530

在第3次和第4次提交中包含错误的代码,但在不知不觉中我已提交并推送了.

In that 3rd and 4th commit contains a wrong code, but unknowingly I committed and pushed.

3.commit 1c4a11a80eb054d24dafec2efed0b0282188e687
  Date:   Sat Dec 5 12:11:50 2015 +0530

4.commit b4ab3c164a3a8d93e0a71a94b5c561cb5e20ebf6
  Date:   Sat Dec 5 12:09:56 2015 +0530

但是第5次和第6次提交包含正确的代码.我需要此提交才能工作.

But 5th and 6th commits contains a correct code. I need this commit to work.

5.commit 167b1d10258381f09663ce61fa88ce3bbcd404c4
  Date:   Sat Dec 5 12:09:21 2015 +0530

6.commit c61bcadac673e1c83f4c14b66d56e23b12fa3198
  Date:   Sat Dec 5 12:07:58 2015 +0530

现在,我想完全删除和删除在第三次和第四次提交中所做的更改.

Now I want to Delete and Remove completely whatever I changed in 3rd and 4th commit.

我想从git历史记录中删除第3次提交和第4次提交.但不是第五名和第六名.

I want delete 3rd and 4th commit from the git history. But not 5th and 6th.

这样我的分支就安全了.

So that my branch will be safe.

推荐答案

您可以使用interactive rebase来返回您的提交历史并以不同的方式进行操作.要使用交互式资源库,只需:

You can use interactive rebase in order to go back in your commit history and do things differently. For using interactive rebase, just:

git rebase -i <commit_id>

<commit_id>是您要编辑的最后一次提交的父级.执行完此命令后,只需将ddrop放在要删除的提交之前,甚至删除与该提交对应的行即可.

<commit_id> is the parent of the last commit you want to edit. After executing this command, just put d or drop in front of commits you're gonna delete or even delete the line corresponding to that commit.

运行此命令将为您提供文本编辑器中的提交列表,如下所示:

Running this command gives you a list of commits in your text editor that looks something like this:

pick f7f3f6d changed my name a bit
pick 310154e updated README formatting and added blame
pick a5f4a0d added cat-file

# Rebase 710f0f8..a5f4a0d onto 710f0f8
#
# Commands:
#  p, pick = use commit
#  r, reword = use commit, but edit the commit message
#  e, edit = use commit, but stop for amending
#  s, squash = use commit, but meld into previous commit
#  f, fixup = like "squash", but discard this commit's log message
#  x, exec = run command (the rest of the line) using shell
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
#
# Note that empty commits are commented out

交互式基础库为您提供了将要运行的脚本.它将从您在命令行上指定的提交开始,然后从上到下重播在每个这些提交中引入的更改.它在顶部列出了最早的,而不是最新的,因为这是它将重播的第一个.

The interactive rebase gives you a script that it’s going to run. It will start at the commit you specify on the command line and replay the changes introduced in each of these commits from top to bottom. It lists the oldest at the top, rather than the newest, because that’s the first one it will replay.

但是,由于您已将分支推送到上游存储库,因此请将该历史重写内容传达给您的可能的同事,如

But since you've pushed the branch to your upstream repository, do communicate this history rewrite to your possible colleagues, as the git documentation says:

再次记住,这是一个变基命令-包括每个提交 范围内的值将被重写,无论您是否更改了 消息与否.不包括您已经推送到任何提交的任何提交 中央服务器–这样做将使其他开发人员感到困惑 相同更改的替代版本.

Remember again that this is a rebasing command – every commit included in the range will be rewritten, whether you change the message or not. Don’t include any commit you’ve already pushed to a central server – doing so will confuse other developers by providing an alternate version of the same change.

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

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