问题描述
我的git历史非常混乱.我要压缩一堆较旧的提交(不包括最后一个).
I have a very messy git history. I want to squash a bunch of older commits (not including the last one).
我知道如何压缩我的上一个n
提交.但这是不同的.在这里,我要连续地将n1
提交到n2
,然后将其压缩为一个,而在n2
之后,我有一个提交的历史记录,我希望保留到最后一个.
I know how to squash my last n
commits. But this is different. Here I have commits consecutively n1
to n2
which I want to squash into one, while after n2
I have a history of commits that I want to preserve up to the last one.
因此,如果我当前的历史记录如下:
So, if my current history looks like this:
---- n1 --- n2 -------- m
我想将n1
压榨到n2
,所以最终看起来像这样:
I want to squash n1
to n2
so it ends up looking like this:
---- n1n2 -------- m
其中n1n2
是单个提交,其中包含从n1
到n2
的压缩内容.
where n1n2
is a single commit containing the squashed contents from n1
to n2
.
我应该怎么做?从n2
到m
的历史记录有什么后果?
How should I do this? What are the consequences on the history from n2
to m
?
也就是说,由于我要执行的操作,从n2
到m
的每次提交的哈希值都会改变吗?
That is, will the hash of every commit from n2
to m
change as a consequence of what I want to do?
推荐答案
-
启动交互式变基:
Start an interactive rebase:
git rebase -i HEAD~n
(其中n
是您想追溯到历史的距离)
(where n
is how far do you want to go back in history)
您的默认编辑器将打开.在顶部,将以相反的顺序显示最新的n
提交列表.例如:
Your default editor will open. At the top, a list of your latest n
commits will be displayed, in reverse order. Eg:
pick a5f4a0d commit-1
pick 19aab46 commit-2
pick 1733ea4 commit-3
pick 827a099 commit-4
pick 10c3f38 commit-5
pick d32d526 commit-6
为要压缩的所有提交指定squash
(或快捷方式s
).例如:
Specify squash
(or the shortcut s
) for all commits you want to squash. E.g.:
pick a5f4a0d commit-1
pick 19aab46 commit-2
squash 1733ea4 commit-3
squash 827a099 commit-4
pick 10c3f38 commit-5
pick d32d526 commit-6
Git会同时应用更改和更改之前的更改,并使您将提交消息合并在一起.
Git applies both that change and the change directly before it and makes you merge the commit messages together.
保存并退出.
Git将应用所有更改,并将再次打开您的编辑器以合并三个提交消息.您可以修改您的提交消息,也可以保持原样(如果这样,所有提交的提交消息将被串联在一起).
Git will apply all changes and will open again your editor to merge the three commit messages. You can modify your commit messages or leave it as it is (if so, the commit messages of all commits will be concatenated).
您完成了!您选择的提交将全部与上一个相同.
You're done! The commits you selected will all be squashed with the previous one.
这篇关于git squash较早的提交(不是最后一个)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!