简体   繁体   English

如何将多个提交合并到另一个分支作为单个压缩提交?

[英]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.如果启用了压缩合并,则“合并”按钮下的下拉列表中应显示“压缩和合并”选项。

“Squash and merge”Github 功能截图

Suppose you worked in feature/task1 with multiple commits.假设您在 feature/task1 中进行了多次提交。

  1. Go to your project branch (project/my_project)转到您的项目分支 (project/my_project)

     git checkout project/my_project
  2. Create a new branch (feature/task1_bugfix)创建一个新分支(feature/task1_bugfix)

     git checkout -b feature/task1_bugfix
  3. Merge with the --squash option--squash选项合并

     git merge --squash feature/task1
  4. Create a single commit创建单个提交

     git commit -am "add single comments"
  5. Push your branch推你的分支

     git push --set-upstream origin feature/task1_bugfix

To squash your local branch before pushing it:在推送之前压缩本地分支:

  1. checkout the branch in question to work on if it is not already checked out.如果尚未签出,请签出相关分支以进行处理。

  2. Find the sha of the oldest commit you wish to keep.找到您希望保留的最旧提交的 sha。

  3. Create/checkout a new branch (tmp1) from that commit.从该提交创建/检出一个新分支 (tmp1)。

    git checkout -b tmp1 <sha1-of-commit>

  4. Merge the original branch into the new one squashing.将原始分支合并到新的压缩分支中。

    git merge --squash <original branch>

  5. Commit the changes which have been created by the merge, with a summary commit message.使用摘要提交消息提交由合并创建的更改。

    git commit -m <msg>

  6. Checkout the original branch you want to squash.签出要压缩的原始分支。

    git checkout <branch>

  7. Reset to the original commit sha you wish to keep.重置为您希望保留的原始提交。

    git reset --soft <sha1>

  8. Rebase this branch based on the new tmp1 branch.根据新的 tmp1 分支重新设置此分支。

    git rebase tmp1

  9. 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 或其他目标分支,只需一次提交

  • Go to merge branch : git checkout master && git pull转到合并分支:git checkout master && git pull
  • Create a work branch from your clean local master : git checkout -b work从干净的本地 master 创建一个工作分支:git checkout -b work
  • Merge squash your feature branch on work : git merge --squash your_feature_branch.在工作中合并挤压您的功能分支:git merge --squash your_feature_branch。
  • Commit with default or a new message : git commit (with a specific or default message)使用默认或新消息提交:git commit(使用特定或默认消息)
  • Go back to your feature branch : git checkout your_feature_branch回到你的功能分支: git checkout your_feature_branch
  • Point your feature branch to work dir : git reset --hard work将您的功能分支指向工作目录:git reset --hard work
  • Verify but you are ready to push : git push -f验证但您已准备好推送:git push -f
  • Then clean up work branch if needed然后根据需要清理工作分支

Replace master with your target branch : develop and so on将 master 替换为您的目标分支:develop 等

  • No need to specify how many commit from your master to your feature branch.无需指定从 master 到 feature 分支的提交次数。 Git takes care* Git 照顾*

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 中签出这个分支 -

  • git fetch混帐
  • git checkout bugfix/123_up git checkout 错误修正/123_up
  • git merge bugfix/123 --squash git 合并错误修复/123 --squash
  • git commit -m "your message" git commit -m "你的消息"
  • git push origin bugfix/123_up git push origin 错误修正/123_up

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.

相关问题 如何仅合并两个提交并压缩一个提交即可合并到另一个分支? - How to merge to another branch with only two commits and with one commit squashed? 压扁提交到功能分支,现在不能干净地合并回 master - Squashed commits onto feature branch and can't have a clean merge back to master now 如何将基于分支的提交合并到另一个分支? - How can I merge branch-based commits into another branch? 如何将1个提交从分支合并到另一个分支 - how can I merge 1 commit from a branch to another branch 如何使用 SmartGIT 在一次提交中压缩合并从分支到主控的多个提交? - How to squash merge multiple commits from branch to master in a single commit using SmartGIT? 在 Git 中,我如何重新设置 + 压缩一个在其历史中具有多个合并提交的分支,而无需挑选到一个全新的分支 - In Git, How can I rebase + squash a branch that has multiple merge commits in it's history without cherry-picking onto a totally new branch 如何使用来自开放 PR 的压缩提交来启动一个新分支? - How can I start a new branch with squashed commits from an open PR? 如何从分支获取所有提交并放入单个提交 - How can get all commits from branch and put in single commit 如何通过Git中的其他推送提交对特定分支进行单个提交? - How can I make a single commit of specific branch from other pushed commits in Git? 我如何 git 将单个提交重新设置为主并推送为新分支 - How can I git rebase a single commit onto master and push as new branch
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM