如何使用 git merge --squash?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5308816/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 10:21:46  来源:igfitidea点击:

How to use git merge --squash?

gitgit-mergegit-squash

提问by SunnyShah

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

  • 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

  • 对于每个错误/功能,我创建了一个不同的 Git 分支

  • 我继续使用非官方的 Git 消息在那个 Git 分支中提交我的代码

  • 在顶级存储库中,我们必须使用官方 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)?

那么如何将我的分支合并到远程分支,以便他们为我的所有签入只获得一次提交(我什至想为此提供提交消息)?

回答by abyx

Say your bug fix branch is called bugfixand 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 bugfixbranch, squash them into 1 commit, and merge it with your masterbranch.

这将从bugfix分支中获取所有提交,将它们压缩为 1 个提交,并将其与您的master分支合并。



Explanation:

说明

git checkout master

Switches to your masterbranch.

切换到您的master分支。

git merge --squash bugfix

Takes all the commits from the bugfixbranch and merges it with your current branch.

bugfix分支中获取所有提交并将其与您当前的分支合并。

git commit

Creates a single commit from the merged changes.

从合并的更改创建单个提交。

Omitting the -mparameter lets you modify a draft commit message containing every message from your squashed commits before finalizing your commit.

省略该-m参数可让您在完成提交之前修改包含来自压缩提交的每条消息的草稿提交消息。

回答by Dan Kohn

What finally cleared this up for me was a commentshowing 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/masterbecause I need to separately resolve merge conflicts for eachof the intermediate commits (or at least the ones which git can't figure out itself). Using git merge --squashgets 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.

当我想将一个功能分支与 105(!!) 个提交合并并将它们全部压缩成一个时,我不想这样做,git rebase -i origin/master因为我需要单独解决每个中间提交的合并冲突(或至少是那些git无法弄清楚自己)。使用git merge --squash可以得到我想要的结果,即合并整个功能分支的单个提交。而且,我最多只需要手动解决一个冲突。

回答by Adam Dymitruk

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

希望这可以帮助

回答by qwertzguy

If you have already git merge bugfixon main, you can squash your merge commit into one with:

如果您已经git merge bugfixmain,您可以使用以下命令将合并提交压缩为一个:

git reset --soft HEAD^1
git commit

回答by Vagelis Prokopiou

Merge newFeaturebranch into masterwith 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

git merge --squash newFeature && git commit

you will get a commit message that will include all the newFeaturebranch commits, which you can customize.

您将收到一条包含所有newFeature分支提交的提交消息,您可以对其进行自定义。

I explain it thoroughly here: https://youtu.be/FQNAIacelT4

我在这里彻底解释:https: //youtu.be/FQNAIacelT4

回答by Aaron

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.

如果启用了压缩合并,则“合并”按钮下的下拉列表中应显示“压缩和合并”选项。

Screenshot of "Squash and merge" Github feature

“Squash and merge”Github 功能截图

回答by Farid Haq

Suppose you worked in feature/task1 with multiple commits.

假设您在 feature/task1 中进行了多次提交。

  1. Go to your project branch (project/my_project)

    git checkout project/my_project
    
  2. Create a new branch (feature/task1_bugfix)

    git checkout -b feature/task1_bugfix
    
  3. Marge with the --squashoption

    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
    
  1. 转到您的项目分支 (project/my_project)

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

    git checkout -b feature/task1_bugfix
    
  3. --squash选择权的玛吉

    git merge --squash feature/task1
    
  4. 创建单个提交

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

    git push --set-upstream origin feature/task1_bugfix
    

回答by ResUta

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]

回答by Demian Berisford-Maynard

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).

然后提交,并完成这个新分支的功能,并删除/忽略旧的(你开发的那个)。

回答by Jool

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.

  3. Create/checkout a new branch (tmp1) from that commit.

    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.

    git rebase tmp1

  9. That's it - now delete the temporary tmp1 branch once you're sure everything is ok.

  1. 如果尚未签出,请签出相关分支以进行处理。

  2. 找到您希望保留的最旧提交的 sha。

  3. 从该提交创建/检出一个新分支 (tmp1)。

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

  4. 将原始分支合并到新分支中。

    git merge --squash <original branch>

  5. 使用摘要提交消息提交由合并创建的更改。

    git commit -m <msg>

  6. 签出要压缩的原始分支。

    git checkout <branch>

  7. 重置为您希望保留的原始提交。

    git reset --soft <sha1>

  8. 根据新的 tmp1 分支重新设置此分支。

    git rebase tmp1

  9. 就是这样 - 现在一旦您确定一切正常,就删除临时 tmp1 分支。