[英]How can I merge multiple commits onto another branch as a single squashed commit?
I have a remote Git server, here is the scenario which I want to perform:我有一个远程 Git 服务器,这是我想要执行的场景:
For each bug/feature I create a different Git branch对于每个错误/功能,我创建了一个不同的 Git 分支
I keep on committing my code in that Git branch with un-official Git messages我继续使用非官方的 Git 消息在那个 Git 分支中提交我的代码
In top repository we have to do one commit for one bug with official Git message在顶级存储库中,我们必须使用官方 Git 消息对一个错误进行一次提交
So how can I merge my branch to remote branch so that they get just one commit for all my check-ins (I even want to provide commit message for this)?那么如何将我的分支合并到远程分支,以便他们为我的所有签入只获得一次提交(我什至想为此提供提交消息)?
Say your bug fix branch is called bugfix
and you want to merge it into master
:假设您的错误修复分支称为
bugfix
并且您想将其合并到master
:
git checkout master
git merge --squash bugfix
git commit
This will take all the commits from the bugfix
branch, squash them into 1 commit, and merge it with your master
branch.这将从错误
bugfix
分支中获取所有提交,将它们bugfix
为 1 个提交,并将其与您的master
分支合并。
Explanation :说明:
git checkout master
Switches to your master
branch.切换到你的
master
分支。
git merge --squash bugfix
Takes all commits from the bugfix
branch and groups it for a 1 commit with your current branch.从
bugfix
分支获取所有提交,并将其分组为与当前分支的 1 次提交。
(no merge commit appears; you could resolve conflicts manually before following commit) (没有出现合并提交;您可以在提交之前手动解决冲突)
git commit
Creates a single commit from the merged changes.从合并的更改创建单个提交。
Omitting the -m
parameter lets you modify a draft commit message containing every message from your squashed commits before finalizing your commit.省略
-m
参数可以让您在完成提交之前修改包含来自压缩提交的每条消息的草稿提交消息。
What finally cleared this up for me was a comment showing that:最终为我澄清的是一条评论,显示:
git checkout main
git merge --squash feature
is the equivalent of doing:相当于做:
git checkout feature
git diff main > feature.patch
git checkout main
patch -p1 < feature.patch
git add .
When I want to merge a feature branch with 105(!!) commits and have them all squashed into one, I don't want to git rebase -i origin/master
because I need to separately resolve merge conflicts for each of the intermediate commits (or at least the ones which git can't figure out itself).当我想将一个功能分支与 105(!!) 个提交合并并将它们全部压缩为一个时,我不想
git rebase -i origin/master
因为我需要单独解决每个中间提交的合并冲突(或者至少是 git 无法弄清楚的那些)。 Using git merge --squash
gets me the result I want, of a single commit for merging an entire feature branch.使用
git merge --squash
可以得到我想要的结果,即合并整个功能分支的单个提交。 And, I only need to do at most one manual conflict resolution.而且,我最多只需要手动解决一个冲突。
You want to merge with the squash option.您想与壁球选项合并。 That's if you want to do it one branch at a time.
那就是如果你想一次做一个分支。
git merge --squash feature1
If you want to merge all the branches at the same time as single commits, then first rebase interactively and squash each feature then octopus merge:如果你想在单个提交的同时合并所有分支,那么首先交互式地变基并压缩每个功能,然后章鱼合并:
git checkout feature1
git rebase -i master
Squash into one commit then repeat for the other features.压缩成一个提交,然后对其他功能重复。
git checkout master
git merge feature1 feature2 feature3 ...
That last merge is an "octopus merge" because it's merging a lot of branches at once.最后一次合并是“章鱼合并”,因为它一次合并了很多分支。
Hope this helps希望这可以帮助
If you have already git merge bugfix
on main
, you can squash your merge commit into one with:如果您已经在
main
上安装了git merge bugfix
,则可以使用以下命令将合并提交压缩为一个:
git reset --soft HEAD^1
git commit
Merge newFeature
branch into master
with a custom commit:使用自定义提交将
newFeature
分支合并到master
:
git merge --squash newFeature && git commit -m 'Your custom commit message';
If instead, you do相反,如果你这样做
git merge --squash newFeature && git commit
you will get a commit message that will include all the newFeature
branch commits, which you can customize.您将收到一条包含所有
newFeature
分支提交的提交消息,您可以对其进行自定义。
I explain it thoroughly here: https://youtu.be/FQNAIacelT4我在这里彻底解释: https : //youtu.be/FQNAIacelT4
I know this question isn't about Github specifically, but since Github is so widely used and this is the answer I was looking for, I'll share it here.我知道这个问题不是专门关于 Github 的,但由于 Github 被广泛使用,这就是我正在寻找的答案,我会在这里分享。
Github has the ability to perform squash merges, depending on the merge options enabled for the repository. Github 能够执行压缩合并,具体取决于为存储库启用的合并选项。
If squash merges are enabled, the "Squash and merge" option should appear in the dropdown under the "Merge" button.如果启用了压缩合并,则“合并”按钮下的下拉列表中应显示“压缩和合并”选项。
Suppose you worked in feature/task1 with multiple commits.假设您在 feature/task1 中进行了多次提交。
Go to your project branch (project/my_project)转到您的项目分支 (project/my_project)
git checkout project/my_project
Create a new branch (feature/task1_bugfix)创建一个新分支(feature/task1_bugfix)
git checkout -b feature/task1_bugfix
Merge with the --squash
option与
--squash
选项合并
git merge --squash feature/task1
Create a single commit创建单个提交
git commit -am "add single comments"
Push your branch推你的分支
git push --set-upstream origin feature/task1_bugfix
To squash your local branch before pushing it:在推送之前压缩本地分支:
checkout the branch in question to work on if it is not already checked out.如果尚未签出,请签出相关分支以进行处理。
Find the sha of the oldest commit you wish to keep.找到您希望保留的最旧提交的 sha。
Create/checkout a new branch (tmp1) from that commit.从该提交创建/检出一个新分支 (tmp1)。
git checkout -b tmp1 <sha1-of-commit>
Merge the original branch into the new one squashing.将原始分支合并到新的压缩分支中。
git merge --squash <original branch>
Commit the changes which have been created by the merge, with a summary commit message.使用摘要提交消息提交由合并创建的更改。
git commit -m <msg>
Checkout the original branch you want to squash.签出要压缩的原始分支。
git checkout <branch>
Reset to the original commit sha you wish to keep.重置为您希望保留的原始提交。
git reset --soft <sha1>
Rebase this branch based on the new tmp1 branch.根据新的 tmp1 分支重新设置此分支。
git rebase tmp1
That's it - now delete the temporary tmp1 branch once you're sure everything is ok.就是这样 - 现在一旦您确定一切正常,就删除临时 tmp1 分支。
For Git对于 Git
Create a new feature创建新功能
via Terminal/Shell:通过终端/外壳:
git checkout origin/feature/<featurename>
git merge --squash origin/feature/<featurename>
This doesnt commit it, allows you to review it first.这不会提交它,允许您先查看它。
Then commit, and finish feature from this new branch, and delete/ignore the old one (the one you did dev on).然后提交,并完成这个新分支的功能,并删除/忽略旧的(你开发的那个)。
git checkout YOUR_RELEASE_BRANCH
git pull
git checkout -b A_NEW_BRANCH
git merge --squash YOUR_BRANCH_WITH_MULTIPLE_COMMITS
git commit -am "squashing all commits into one"
git push --set-upstream origin A_NEW_BRANCH
if you get error: Committing is not possible because you have unmerged files.如果出现错误:无法提交,因为您有未合并的文件。
git checkout master
git merge --squash bugfix
git add .
git commit -m "Message"
fixed all the Conflict files修复了所有冲突文件
git add .
you could also use你也可以使用
git add [filename]
Your feature branch is done and ready to commit to master, develop or other target branch with only one commit您的功能分支已完成并准备好提交到 master、develop 或其他目标分支,只需一次提交
Replace master with your target branch : develop and so on将 master 替换为您的目标分支:develop 等
Assume the name of the branch where you made multiple commits is called bugfix/123, and you want to squash these commits.假设您进行多次提交的分支的名称称为 bugfix/123,并且您想压缩这些提交。
First, create a new branch from develop (or whatever the name of your repo is).首先,从 develop 创建一个新分支(或者无论你的仓库名称是什么)。 Assume the name of the new branch is called bugfix/123_up.
假设新分支的名称为 bugfix/123_up。 Checkout this branch in git bash -
在 git bash 中签出这个分支 -
Now this branch will have only one commit with all your changes in it.现在这个分支将只有一个包含所有更改的提交。
You can use tool I've created to make this process easier: git-squash .您可以使用我创建的工具来简化此过程: git-squash 。 For example to squash all commits on feature branch that has been branched from master branch, write:
例如,要压缩从主分支分支的功能分支上的所有提交,请编写:
git squash master
git push --force
Use用
git status
to check what's going on.检查发生了什么。
Then然后
git checkout master
git merge --squash bugfix
git add (add which files you want or use wildcard command like ".")
Then然后
git commit -m "message"
And now last but not the least现在最后但并非最不重要
git push -u origin master
Here origin
can be other remote you prefer.这里的
origin
可以是您喜欢的其他遥控器。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.