Git:如何压缩分支上的所有提交 [英] Git: How to squash all commits on branch

查看:63
本文介绍了Git:如何压缩分支上的所有提交的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从 master 创建新分支:

git checkout -b testbranch

我提交了 20 次.

现在我想压缩这 20 个提交.我这样做:

git rebase -i HEAD~20

如果我不知道有多少提交怎么办?有什么办法可以做这样的事情:

git rebase -i all 在这个分支上

解决方案

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

git checkout yourBranchgit reset $(git merge-base master $(git branch --show-current))git add -Agit commit -m "在你的分支上提交一次";

这并不完美,因为它意味着您知道yourBranch"来自哪个分支.来自.
注意:发现 origin 分支不是使用 Git 容易/可能(视觉方式通常是最简单的,如见此处).

注意:git branch --show-current随 Git 2.22(20219 年第一季度)一起引入.


您需要使用 git push --force


Karlotcha Hoa 添加了 在评论中:

<块引用>

对于重置,你可以这样做

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

<块引用>

[那个] 自动使用你当前所在的分支.
如果您使用它,您也可以使用别名,因为该命令不依赖于分支名称.

I make new branch from master with:

git checkout -b testbranch

I make 20 commits into it.

Now I want to squash those 20 commits. I do that with:

git rebase -i HEAD~20

What about if I don't know how many commits? Is there any way to do something like:

git rebase -i all on this branch

解决方案

Another way to squash all your commits is to reset the index to master:

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

This isn't perfect as it implies you know from which branch "yourBranch" is coming from.
Note: finding that origin branch isn't easy/possible with Git (the visual way is often the easiest, as seen here).

Note: git branch --show-current has been introduced with Git 2.22 (Q1 20219).


EDIT: you will need to use git push --force


Karlotcha Hoa adds in 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.

这篇关于Git:如何压缩分支上的所有提交的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆