如何将多个提交合并到另一个分支作为单个压缩提交?
我有一个远程 Git 服务器,这是我想要执行的场景:
对于每个错误/功能,我创建一个不同的 Git 分支
我继续使用非官方 Git 消息在该 Git 分支中提交我的代码
在顶级存储库中,我们必须针对一个错误进行一次提交带有官方 Git 消息
那么我如何将我的分支合并到远程分支,以便他们只为我的所有签入获得一次提交(我什至想要为此提供提交消息)?
I have a remote Git server, here is the scenario which I want to perform:
For each bug/feature I create a different Git branch
I keep on committing my code in that Git branch with un-official Git messages
In top repository we have to do one commit for one bug with official Git message
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)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(15)
如果出现错误:无法提交,因为您有未合并的文件。
所有冲突文件
修复了您也可以使用的
if you get error: Committing is not possible because you have unmerged files.
fixed all the Conflict files
you could also use
您的功能分支已完成,只需一次提交即可提交到 master、develop 或其他目标分支
将 master 替换为目标分支:开发等
Your feature branch is done and ready to commit to master, develop or other target branch with only one commit
Replace master with your target branch : develop and so on
假设您进行多次提交的分支名称称为 bugfix/123,并且您想要压缩这些提交。
首先,从开发创建一个新分支(或者无论您的存储库名称是什么)。假设新分支的名称为 bugfix/123_up。在 git bash 中签出此分支 -
现在,该分支将只有一个提交,其中包含您的所有更改。
Assume the name of the branch where you made multiple commits is called bugfix/123, and you want to squash these commits.
First, create a new branch from develop (or whatever the name of your repo is). Assume the name of the new branch is called bugfix/123_up. Checkout this branch in git bash -
Now this branch will have only one commit with all your changes in it.
您可以使用我创建的工具来简化此过程:git-squash。例如,要压缩从 master 分支分支的功能分支上的所有提交,请编写:
You can use tool I've created to make this process easier: git-squash. For example to squash all commits on feature branch that has been branched from master branch, write:
用于
检查发生了什么。
然后
然后
现在最后但并非最不重要的
这里
origin
可以是您喜欢的其他远程。Use
to check what's going on.
Then
Then
And now last but not the least
Here
origin
can be other remote you prefer.假设您的错误修复分支名为
bugfix
并且您想将其合并到master
中:这将获取来自
bugfix
分支的所有提交,squash将它们合并到 1 个提交中,并将其与您的master
分支合并。说明:
切换到您的
master
分支。从
bugfix
分支获取所有提交,并将其分组为当前分支的 1 次提交。(没有出现合并提交;您可以在提交之前手动解决冲突)
从合并的更改创建单个提交。
省略
-m
参数可让您在最终提交之前修改草稿提交消息,其中包含压缩提交中的每条消息。Say your bug fix branch is called
bugfix
and you want to merge it intomaster
:This will take all the commits from the
bugfix
branch, squash them into 1 commit, and merge it with yourmaster
branch.Explanation:
Switches to your
master
branch.Takes all commits from the
bugfix
branch and groups it for a 1 commit with your current branch.(no merge commit appears; you could resolve conflicts manually before following 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.最终为我解决这个问题的是 评论 表明:
相当于执行以下操作:
当我想要合并具有 105(!!) 次提交的功能分支并将它们全部压缩为一个时,我不想 git rebase -i origin/master ,因为我需要单独解析合并每个中间提交(或者至少是 git 无法弄清楚的提交)都有冲突。使用 git merge --squash 可以获得我想要的结果,即用于合并整个功能分支的单个提交。而且,我最多只需要进行一次手动冲突解决。
What finally cleared this up for me was a comment showing that:
is the equivalent of doing:
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). Usinggit merge --squash
gets me the result I want, of a single commit for merging an entire feature branch. 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.
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:
Squash into one commit then repeat for the other features.
That last merge is an "octopus merge" because it's merging a lot of branches at once.
假设您在 feature/task1 中进行了多次提交。
转到您的项目分支 (project/my_project)
创建一个新分支 (feature/task1_bugfix)
与
--squash
选项合并创建单个提交
推送您的分支
Suppose you worked in feature/task1 with multiple commits.
Go to your project branch (project/my_project)
Create a new branch (feature/task1_bugfix)
Merge with the
--squash
optionCreate a single commit
Push your branch
使用自定义提交将
newFeature
分支合并到master
中:如果相反,您可以执行
git merge --squash newFeature && git commit
您将收到一条提交消息,其中包含您可以自定义的所有
newFeature
分支提交。我在这里彻底解释一下:https://youtu.be/FQNAIacelT4
Merge
newFeature
branch intomaster
with a custom commit: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.I explain it thoroughly here: https://youtu.be/FQNAIacelT4
如果您已经在
main
上进行了git merge bugfix
,则可以使用以下命令将合并提交压缩为一个:If you have already
git merge bugfix
onmain
, you can squash your merge commit into one with:我知道这个问题并不是专门针对 Github 的,但由于 Github 的使用非常广泛,而且这就是我一直在寻找的答案,所以我将在这里分享。
Github 能够执行压缩合并,具体取决于存储库启用的合并选项。
如果启用了挤压合并,则“挤压并合并”选项应出现在“合并”按钮下方的下拉列表中。
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 has the ability to perform squash merges, depending on the merge options enabled for the repository.
If squash merges are enabled, the "Squash and merge" option should appear in the dropdown under the "Merge" button.
要在推送之前压缩本地分支:
如果尚未签出,请签出要处理的相关分支。
查找您希望保留的最旧提交的 sha。
从该提交创建/签出一个新分支 (tmp1)。
git checkout -b tmp1
将原始分支合并到新分支中。
git merge --squash <原始分支>
提交合并创建的更改,并带有摘要提交消息。
git commit -m
签出您想要压缩的原始分支。
git checkout
重置为您希望保留的原始提交 sha。
git reset --soft
根据新的 tmp1 分支重新设置此分支的基础。
git rebase tmp1
就是这样 - 现在,一旦您确定一切正常,就删除临时 tmp1 分支。
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.
Create/checkout a new branch (tmp1) from that commit.
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.
git rebase tmp1
That's it - now delete the temporary tmp1 branch once you're sure everything is ok.
对于 Git
创建新功能
通过终端/Shell
:这不会提交它,允许您先查看它。
然后提交并完成这个新分支的功能,并删除/忽略旧分支(您进行开发的分支)。
For Git
Create a new feature
via Terminal/Shell:
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).