[英]How to squash commits in one branch?
I made a branch of trunk and been submitting/committing lots of changes.我做了一个主干分支并提交/提交了很多更改。 Every so often I would update the branch by merging from trunk (eg. 20 commits, then a merge-update, 20 more commits then a merge-update, etc).
每隔一段时间,我就会通过从主干合并来更新分支(例如,20 次提交,然后是合并更新,再有 20 次提交然后是合并更新等)。
Now I would like to squash everything in my branch.现在我想压扁我分支中的所有东西。 How can I do this (using either Git Extensions or console)?
我该怎么做(使用 Git 扩展或控制台)?
I tried typing:我试着打字:
git rebase -i HEAD~200
but I don't know how many commits to squash.但我不知道有多少提交给壁球。 I tried counting the commits in Git Extensions but it's hard to see since it shows a mixture of everything from branch commits and trunk commits.
我尝试计算 Git Extensions 中的提交,但很难看到,因为它显示了分支提交和主干提交的所有内容的混合。 The menu "Show current branch only" doesn't help.
菜单“仅显示当前分支”没有帮助。
To rebase all commits made since branching out from master
you might use the following command:要对从
master
分支出来后所做的所有提交进行 rebase,您可以使用以下命令:
git rebase -i `git merge-base HEAD master`
git merge-base
finds the closest common ancestor between your current branch and the master (ie the last commit that is available on both). git merge-base
查找当前分支和主分支之间最近的共同祖先(即两者都可用的最后一次提交)。
After passing it to the git rebase
you get list of all commits since branching out from master
(or whatever branch you will put in there) which you can freely squash.将它传递给
git rebase
您将获得从master
分支(或您将放入的任何分支)分支出来的所有提交的列表,您可以自由地压缩这些提交。
Note: this will not differentiate between yours and merge commits, but as far as I understand that's not what you're looking for.注意:这不会区分您的提交和合并提交,但据我所知,这不是您要查找的内容。
Note 2: beware that rewriting history after pushing to remote repo might require force-pushing, which is very troublesome when working on a single branch with other people.注意2:要注意推送到远程repo后重写历史可能需要强制推送,这在与其他人在单个分支上工作时非常麻烦。
Since you need to squash so many commits, you can use the way below:既然你需要压缩这么多的提交,你可以使用下面的方法:
Assume myBranch
original like:假设
myBranch
原来像:
...M---A---B---...---N---...---X myBranch
If you need to squash commits from A
to X
, you just need to find the parent of commit A
(as commit M
in above graph), and then use the commands如果您需要将提交从
A
压缩到X
,您只需要找到提交A
的父A
(如上图中的提交M
),然后使用命令
git checkout myBranch
git reset --soft <commit id for M>
git commit -m 'squash commit from A to X'
Then the commits on myBranch
will be (the squash commit is S
):然后
myBranch
上的提交将是(壁球提交是S
):
...M---S myBranch
此代码将以交互模式重新设置您当前的分支提交,并将重新设置的提交放在 master 之上
git rebase -i HEAD~$(git rev-list --count origin/master..) && git pull --rebase origin master
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.