你如何删除git历史记录中的特定版本? [英] How do you remove a specific revision in the git history?

查看:1109
本文介绍了你如何删除git历史记录中的特定版本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设你的git历史记录如下所示:

<1>
2
3
4
5



1-5是单独的修订版本。你需要删除3,同时仍然保持1,2,4和5.这怎么能做到?



有没有一种有效的方法,当数以百计的修改后一个被删除?

解决方案

要将修订版本3和4合并到一个修订版本中,可以使用git rebase。如果您想删除修订版本3中的更改,则需要在交互式重新分配模式下使用编辑命令。如果您想将更改合并到一个修订版中,请使用压扁。



我已成功使用此压扁技术,但从未需要先删除修订版。 拆分提交下的git-rebase文档应该有希望给你足够的想法。 (或其他人可能知道)。



git documentation
$ b


从最旧的提交开始,您要保留原样:

git rebase -i

编辑器将会触发当前分支中的所有提交(忽略合并提交),这些提交会在给定提交之后提交。您可以将此列表中的提交重新排序,直至您的内容为准,并且可以将其删除。这个列表看起来或多或少是这样的:

 pick deadbee这个提交的直线
选择fa1afe1下一个提交的直线
...

oneline描述纯粹是为了您的乐趣; git-rebase不会查看它们,而是以提交名称(本例中为deadbee和fa1afe1),因此不要删除或编辑这些名称。



通过使用命令edit替换命令pick,可以告诉git-rebase在应用该提交后停止,以便您可以编辑文件和/或提交消息,修改提交并继续重新绑定。 / p>

如果要将两个或多个提交合并为一个,请将pick命令替换为squash以进行第二次提交。如果提交具有不同的作者,它会将压扁的提交归因于第一次提交的作者。



Suppose your git history looks like this:

1 2 3 4 5

1–5 are separate revisions. You need to remove 3 while still keeping 1, 2, 4 and 5. How can this be done?

Is there an efficient method when there are hundreds of revisions after the one to be deleted?

解决方案

To combine revision 3 and 4 into a single revision, you can use git rebase. If you want to remove the changes in revision 3, you need to use the edit command in the interactive rebase mode. If you want to combine the changes into a single revision, use squash.

I have successfully used this squash technique, but have never needed to remove a revision before. The git-rebase documentation under "Splitting commits" should hopefully give you enough of an idea to figure it out. (Or someone else might know).

From the git documentation:

Start it with the oldest commit you want to retain as-is:

git rebase -i <after-this-commit>

An editor will be fired up with all the commits in your current branch (ignoring merge commits), which come after the given commit. You can reorder the commits in this list to your heart's content, and you can remove them. The list looks more or less like this:

pick deadbee The oneline of this commit
pick fa1afe1 The oneline of the next commit
...

The oneline descriptions are purely for your pleasure; git-rebase will not look at them but at the commit names ("deadbee" and "fa1afe1" in this example), so do not delete or edit the names.

By replacing the command "pick" with the command "edit", you can tell git-rebase to stop after applying that commit, so that you can edit the files and/or the commit message, amend the commit, and continue rebasing.

If you want to fold two or more commits into one, replace the command "pick" with "squash" for the second and subsequent commit. If the commits had different authors, it will attribute the squashed commit to the author of the first commit.

这篇关于你如何删除git历史记录中的特定版本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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