英文:
git squash commits from start to end
问题
假设我有一个分支和5个提交。我已经做了一些更改并注意到,从第2到第4个提交都与同一个修复相关,我想将它们压缩成一个单独的提交。但是,我希望保持边界之外的一切不受影响。是否可以实现这样的行为?
这里是一个示例:
<sha-5> 提交 {5} <---- 这个应该保持不变
|
|
|
<sha-4> 提交 {4} <---- 从这个提交开始压缩
|
|
|
<sha-3> 提交 {3}
|
|
|
<sha-2> 提交 {2} <---- 到这个提交
|
|
|
<sha-1> 提交 {1}. <--- 之前的所有内容,包括此提交,也应该保持不变
英文:
Suppose I have a branch and 5 commits. I've done some changes and noticed, that commits in range from 2 to 4 are relevant to the same fix and I'd like to squash them into a single one. Hovewer, I want to leave everything outside the borders untouched. Is it possible to achieve such behaviour?
Here is an example:
<sha-5> commit {5} <---- this one should be untouched
|
|
|
<sha-4> commit {4} <---- squash from this one
|
|
|
<sha-3> commit {3}
|
|
|
<sha-2> commit {2} <---- to this one
|
|
|
<sha-1> commit {1}. <--- everything that goes before, including this commit should be untouched as well
答案1
得分: 0
你可以使用 git rebase -i
来开始一个交互式的 rebase 会话。在这种情况下,你应该从你想要合并的第一个提交的父提交开始它。
以下是如何操作:
-
你需要获取在你想要合并的提交之前的提交的 SHA,就像这个例子中提交 {1} 的 SHA 一样。我们称它为
<sha-1>
。 -
使用这个 SHA 开始一个交互式 rebase 会话:
git rebase -i <sha-1>
-
在打开的文本编辑器中,你会看到一个包含在你指定的提交和最新提交之间的提交列表。它应该看起来类似于这样:
pick <sha-2> commit {2} pick <sha-3> commit {3} pick <sha-4> commit {4} pick <sha-5> commit {5}
-
你想要合并提交 {2} 到 {4},所以你应该将这些行上的
pick
改为squash
(或者缩写为s
)。你要合并的组的第一个提交应该保持为pick
,而后续的提交应该标记为squash
。现在应该看起来像这样:pick <sha-2> commit {2} squash <sha-3> commit {3} squash <sha-4> commit {4} pick <sha-5> commit {5}
-
保存并关闭编辑器(如果你在 vim 中,你可以输入
:wq
并按 Enter 键)。 -
一个编辑器窗口会打开,让你修改新合并提交的提交消息。在输入新提交消息后,保存并关闭编辑器。
-
Git 然后会重新应用跟在你的合并提交后面的提交。在这种情况下,是提交 {5}。
-
如果在合并提交和重新应用的提交之间存在冲突,Git 会暂停并允许你在继续之前解决这些冲突。
通过这个方法,你成功地合并了你的提交。你可以使用 git log
来验证这一点,在其中你现在应该看到了合并的提交和提交 {5},以及提交 {2} 之前的所有提交。
英文:
You can use git rebase -i
to start an interactive rebase session. In this case, you would want to start it from the parent of the first commit you want to squash.
Here's how to do it:
-
You need to get the SHA of the commit before the commits you want to squash, in this case that's the SHA of commit {1}. Let's call it
<sha-1>
. -
Start an interactive rebase session using this SHA:
git rebase -i <sha-1>
-
In the text editor that opens, you will see a list of commits between the one you specified and the latest commit. It should look something like this:
pick <sha-2> commit {2} pick <sha-3> commit {3} pick <sha-4> commit {4} pick <sha-5> commit {5}
-
You want to squash commits {2} to {4}, so you should change
pick
tosquash
(ors
for short) on those lines. The first commit of the group you want to squash should remain aspick
, and all the following ones should be marked assquash
. It should now look like this:pick <sha-2> commit {2} squash <sha-3> commit {3} squash <sha-4> commit {4} pick <sha-5> commit {5}
-
Save and close the editor (if you're in vim, you can do this by typing
:wq
and hitting Enter). -
An editor window will open for you to change the commit message of the new squashed commit. After you’ve entered the new commit message, save and close the editor.
-
Git will then reapply the commits that followed your squashed commits. In this case, commit {5}.
-
If there are conflicts between the squashed commit and the reapplied commits, Git will pause and allow you to resolve those conflicts before continuing.
With this, you have successfully squashed your commits. You can verify this using git log
, where you should now see the squashed commit and commit {5}, plus all the commits before commit {2}.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论