我用:
git checkout -b testbranch
我做了20次提交。
现在我想要压缩这20个提交。我是这样做的:
git rebase -i HEAD~20
如果我不知道有多少次提交呢?有没有什么方法可以做到:
git rebase -i all on this branch
我用:
git checkout -b testbranch
我做了20次提交。
现在我想要压缩这20个提交。我是这样做的:
git rebase -i HEAD~20
如果我不知道有多少次提交呢?有没有什么方法可以做到:
git rebase -i all on this branch
当前回答
所有这些git重置,硬的,软的,以及这里提到的所有其他东西都可能工作(它不适合我),如果你正确地执行步骤和某种精灵。 如果你是普通的smo,试试这个: 如何使用git合并-squash?
Saved my life, and will be my go to squash, been using this 4 times since I found out about it. Simple, clean and basically 1 comamnd. In short: If you are on a branch lets call it "my_new_feature" off develop and your pull request has 35 commits (or however many) and you want it to be 1. A. Make sure your branch is up to date, Go on develop, get latest and merge and resolve any conflicts with "my_new_feature" (this step really you should take as soon as you can all the time anyway) B. Get latest of develop and branch out to a new branch call it "my_new_feature_squashed"
C.魔法在这里。 你想把你的工作从“my_new_feature”变成“my_new_feature_squashed” 所以只要这样做(在我们创建off develop的新分支上): Git merge—squash my_new_feature
你所有的改变现在都在你的新分支上,你可以自由地测试它,然后只做一个单一的提交,推送,那个分支的新PR——然后等待第二天重复。 你不喜欢编程吗?:)
其他回答
另一种简单的方法是:在原始分支上进行合并-挤压。该命令不执行“压缩”提交。当你这样做时,你分支的所有提交消息将被收集。
$ git checkout master
$ git merge --squash yourBranch
$ git commit # all commit messages of yourBranch in one, really useful
> [status 5007e77] Squashed commit of the following: ...
git checkout -b temp
git checkout yourbranch
git fetch
git reset --hard origin/master
git merge --squash temp
git commit -m "new message"
最简单的方法。
这将创建一个新的分支,然后将你的分支重置为基础分支,然后在将临时分支合并回我们的分支之前,我们压缩更改并创建一个新的提交
在阅读了几个关于压缩的Stackoverflow问题和答案后,我认为这是一个很好的压缩分支上所有提交的代码行:
git reset --soft $(git merge-base master YOUR_BRANCH) && git commit -am "YOUR COMMIT MESSAGE" && git rebase -i master
这是假设master是基础分支。
你可以使用我专门为这个任务创建的工具:
https://github.com/sheerun/git-squash
基本上你需要打电话给git壁球大师,你就搞定了
在之前的回答中,我没有看到任何关于如何处理“凌乱的分支”和“自我冲突”的信息。例如,我经常在我的特性分支(称为特性)上有主提交,这导致了它们之间的冲突。我发现这是最难处理的问题之一。
如何挤压凌乱的树枝?使用临时分支!
我发现Felix Rieseberg的解决方案是最好的。以下是我对他的建议略短的抄写:
Create a local tmp branch of off master git checkout master && git pull && git checkout -b tmp Merge all feature changes into tmp (without any commits, only staged file changes). git merge --squash $feature Manually solve all remaining "real conflicts" (This is the only step you cannot have a script do for you) Commit. tmp is now master + 1 commit (containing all changes). git commit ... Checkout feature and git reset --hard tmp (feature's original contents are gone, and it is now basically tmp, but renamed) git checkout $feature && git reset --hard tmp Ignore and override origin/feature (then clean up) git push -f && git branch -D tmp
Felix指出,这将产生最干净的合并,没有任何来自master和feature之间混乱/复杂关系的奇怪的自我冲突:
您可能会遇到一些不可避免的合并冲突。请相信,这是尽可能少的冲突,因为您跳过了最初创建的许多中间提交。