简体   繁体   English

我一次只能压2次提交吗?

[英]Can I only squash 2 commits at a time?

When I try to interactively rebase (squash) several commits into one, it only ever squashes the last two commits so I end up having to do the squash several times. 当我尝试以交互方式将多个提交变基为一个(压榨)时,它仅压榨最后两个提交,因此我最终不得不多次压榨。

I am using source tree, but I have tried doing it from the console with the same results. 我正在使用源代码树,但我尝试从控制台执行此操作,结果相同。 Any ideas? 有任何想法吗?

Try - 尝试-

$ git rebase -i HEAD~4

then 然后

pick 01d1124 commit msg 1
squash 6340aaa commit msg 2
squash ebfd367 commit msg 3
squash 30e0ccb commit msg 4

save and quit your editor 保存并退出编辑器

You can do the same in Sourcetree - 您可以在Sourcetree中执行相同的操作-

http://kwnetzwelt.net/wordpress/archives/2440 http://kwnetzwelt.net/wordpress/archives/2440

(Interactive) rebasing technically squashes only two commits at a time—simply because the so-called "rebase script" you're typically asked to edit before git rebase proceeds applies an action to a single commit at a time. (交互性)基于技术的重新整理一次只能压缩两次提交-这是因为通常会要求您在git rebase进行之前编辑所谓的“重新编写脚本”,一次将操作应用于一次提交。

This does not, however, change the outcome because logically there's no difference between squashing N commits at once or doing this commit-by-commit. 但是,这不会改变结果,因为从逻辑上讲,一次压缩N个提交或一次提交一次提交之间没有区别。 In your rebase script, you just pick the action "squash" (also read up on "fixup"—it's often may be more handy than "squash") for a series of adjacent commits, and then git rebase works like this: 在您的rebase脚本中,您只需为一系列相邻的提交选择动作“ squash”(也请阅读“ fixup”,它通常比“ squash”更方便),然后git rebase工作方式如下:

  1. Get the commit marked "squash". 获取标记为“ squash”的提交。 Squash it to the previous commit—effectively re-writiing that with the new content applied to it (as with git apply ). 将其压入上一个提交-有效地将其重新应用新内容(如git apply )。
  2. Given that new state, go to step 1. ;-) 给定新状态,请转到步骤1。

Note that there's also the --squash command-line option of git merge . 注意,还有git merge--squash命令行选项。 This one squashes several commits at once for real. 这一次真正压缩了多个提交。

Given that your branch points to the last of the commits you want to squash you can use this trick: 鉴于您的分支指向您要压榨的最后提交,您可以使用以下技巧:

git reset --soft <some older commit>
git commit --amend

You have now replaced <some older commit> with a new one containing everything between the two, plus their content. 现在,您已经用一个包含两者之间的所有内容及其内容的新内容替换了<some older commit>

Squash 壁球

This is exactly what interactive rebase is for. 这正是交互式变基的目的。

You use the git rebase -i @~x where x stand for the number of commits you wish to squash. 您可以使用git rebase -i @~x ,其中x代表您希望压榨的提交次数。

Then a dialog will open up for you and you can choose commits and what you wish to do with each commit. 然后将为您打开一个对话框,您可以选择提交以及您希望对每个提交执行的操作。

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM