Git:如何压缩分支上的所有提交

标签 git version-control

我从 master 创建新分支:

git checkout -b testbranch

我对其进行了 20 次提交。

现在我想压缩这 20 次提交。我用:

git rebase -i HEAD~20

如果我不知道有多少次提交怎么办?有没有办法做类似的事情:

git rebase -i all on this branch

最佳答案

压缩所有提交的另一种方法是将索引重置为 master:

git checkout yourBranch
git reset $(git merge-base master $(git branch --show-current))
git add -A
git commit -m "one commit on yourBranch"

这并不完美,因为它意味着您知道“yourBranch”来自哪个分支。
注意:发现 origin 分支不是 easy/possible with Git (visual way is often the easiest ,作为 seen here )。

注意:git branch --show-current 已经是introduced with Git 2.22 (Q2 2019) .


编辑:您将需要使用 git push --force


Karlotcha Hoa添加 the comments :

For the reset, you can do

git reset $(git merge-base master $(git rev-parse --abbrev-ref HEAD)) 

[That] automatically uses the branch you are currently on.
And if you use that, you can also use an alias, as the command doesn't rely on the branch name.


sschoof添加 the comments :

Since my default branch is called main and my search had multi times brought me here:
To copy it for my next time

git reset $(git merge-base main $(git rev-parse --abbrev-ref HEAD))

关于Git:如何压缩分支上的所有提交,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25356810/

相关文章:

Git rebase 出现分歧

git - 如何获取所有远程分支?

git - 语义版本控制和 git 分支

c# - 防止连接字符串被提交到存储库

c++ - 跟踪可执行文件中的代码版本

git - 即使 curl 适用于 https 页面,也无法克隆 https 存储库

version-control - 如何维护多种代码开发路径?

gitosis 和 git clone 问题

version-control - 对 RTF 文档使用版本控制?

c++ - LINUX下AIX上的 'what'命令是什么