合并从开始到结束的 Git 提交。

huangapple go评论55阅读模式
英文:

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:

&lt;sha-5&gt; commit {5}        &lt;---- this one should be untouched
|
|
|
&lt;sha-4&gt; commit {4}        &lt;---- squash from this one 
|
|
|
&lt;sha-3&gt; commit {3}
|
|
|
&lt;sha-2&gt; commit {2}        &lt;---- to this one
|
|
|
&lt;sha-1&gt; commit {1}.       &lt;--- everything that goes before, including this commit should be untouched as well

答案1

得分: 0

你可以使用 git rebase -i 来开始一个交互式的 rebase 会话。在这种情况下,你应该从你想要合并的第一个提交的父提交开始它。

以下是如何操作:

  1. 你需要获取在你想要合并的提交之前的提交的 SHA,就像这个例子中提交 {1} 的 SHA 一样。我们称它为 <sha-1>

  2. 使用这个 SHA 开始一个交互式 rebase 会话:

    git rebase -i <sha-1>
    
  3. 在打开的文本编辑器中,你会看到一个包含在你指定的提交和最新提交之间的提交列表。它应该看起来类似于这样:

    pick <sha-2> commit {2}
    pick <sha-3> commit {3}
    pick <sha-4> commit {4}
    pick <sha-5> commit {5}
    
  4. 你想要合并提交 {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}
    
  5. 保存并关闭编辑器(如果你在 vim 中,你可以输入 :wq 并按 Enter 键)。

  6. 一个编辑器窗口会打开,让你修改新合并提交的提交消息。在输入新提交消息后,保存并关闭编辑器。

  7. Git 然后会重新应用跟在你的合并提交后面的提交。在这种情况下,是提交 {5}。

  8. 如果在合并提交和重新应用的提交之间存在冲突,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:

  1. 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 &lt;sha-1&gt;.

  2. Start an interactive rebase session using this SHA:

    git rebase -i &lt;sha-1&gt;
    
  3. 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 &lt;sha-2&gt; commit {2}
    pick &lt;sha-3&gt; commit {3}
    pick &lt;sha-4&gt; commit {4}
    pick &lt;sha-5&gt; commit {5}
    
  4. You want to squash commits {2} to {4}, so you should change pick to squash (or s for short) on those lines. The first commit of the group you want to squash should remain as pick, and all the following ones should be marked as squash. It should now look like this:

    pick &lt;sha-2&gt; commit {2}
    squash &lt;sha-3&gt; commit {3}
    squash &lt;sha-4&gt; commit {4}
    pick &lt;sha-5&gt; commit {5}
    
  5. Save and close the editor (if you're in vim, you can do this by typing :wq and hitting Enter).

  6. 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.

  7. Git will then reapply the commits that followed your squashed commits. In this case, commit {5}.

  8. 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}.

huangapple
  • 本文由 发表于 2023年6月18日 19:11:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/76500238.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定