我用:
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重置,正如之前在许多回答中提到的,是迄今为止实现你想要的最好和最简单的方法。我在以下工作流程中使用它:
(有关发展分支)
git fetch
git merge origin/master #so development branch has all current changes from master
git reset origin/master #will show all changes from development branch to master as unstaged
git gui # do a final review, stage all changes you really want
git commit # all changes in a single commit
git branch -f master #update local master branch
git push origin master #push it
其他回答
你可以通过子命令来实现
$ git rebase -i HEAD~$(git rev-list -count HEAD ^master)
这将首先计算从master分离后的提交次数,然后将其还原到确切的长度。
另一种简单的方法是:在原始分支上进行合并-挤压。该命令不执行“压缩”提交。当你这样做时,你分支的所有提交消息将被收集。
$ 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: ...
你可以使用我专门为这个任务创建的工具:
https://github.com/sheerun/git-squash
基本上你需要打电话给git壁球大师,你就搞定了
如果你可以接受涉及另一个分支的答案,尝试git checkout——orphan <new_branch>它允许我简单地将前一个分支的所有文件作为一个提交。
这有点像git的合并压缩,但不完全相同。
你正在做的事情很容易出错。只做:
git rebase -i master
它会自动将你的分支的提交重置到当前最新的主节点上。