本文介绍了合并 --squash 和变基有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 git 的新手,我试图了解壁球和变基之间的区别.据我了解,您在进行 rebase 时会进行壁球.

I'm new to git and I'm trying to understand the difference between a squash and a rebase. As I understand it you perform a squash when doing a rebase.

推荐答案

git merge --squashgit rebase --interactive 都可以产生压扁"的提交.
但它们的用途不同.

Both git merge --squash and git rebase --interactive can produce a "squashed" commit.
But they serve different purposes.

将在目标分支上产生压缩提交,不标记任何合并关系.
(注意:它不会立即产生提交:你需要一个额外的 git commit -m "squash branch")
如果您想完全丢弃源分支,这很有用,从 (schema 取自 所以问题):

will produce a squashed commit on the destination branch, without marking any merge relationship.
(Note: it does not produce a commit right away: you need an additional git commit -m "squash branch")
This is useful if you want to throw away the source branch completely, going from (schema taken from SO question):

 git checkout stable

      X                   stable
     /
a---b---c---d---e---f---g tmp

到:

git merge --squash tmp
git commit -m "squash tmp"

      X-------------------G stable
     /
a---b---c---d---e---f---g tmp

然后删除tmp分支.

注意:git merge 有一个 --commit 选项,但它不能与 --squash 一起使用.永远不可能同时使用 --commit--squash.
自 Git 2.22.1(2019 年第 3 季度)以来,这种不兼容性已明确:

Note: git merge has a --commit option, but it cannot be used with --squash. It was never possible to use --commit and --squash together.
Since Git 2.22.1 (Q3 2019), this incompatibility is made explicit:

参见 commit 1d14d0c(2019 年 5 月 24 日 https),作者:Vishal Verma (reloadbrain).

See commit 1d14d0c (24 May 2019) by Vishal Verma (reloadbrain).

以前,当提供 --squash 时,'option_commit' 是静默的掉了.对于试图覆盖的用户来说,这可能会令人惊讶明确使用 --commit 的壁球无提交行为.

git/gitbuiltin/merge.c#cmd_merge() 现在包括:

if (option_commit > 0)
    die(_("You cannot combine --squash with --commit."));

  • git rebase --interactive

    在新的基础上重放您的部分或全部提交,允许您压缩(或最近修复",请参阅此 SO 问题),直接转到:

    replays some or all of your commits on a new base, allowing you to squash (or more recently "fix up", see this SO question), going directly to:

    git checkout tmp
    git rebase -i stable
    
          stable
          X-------------------G tmp
         /
    a---b
    

    如果您选择压缩 tmp 的所有提交(但是,与 merge --squash 相反,您可以选择重放一些,并压缩其他).

    If you choose to squash all commits of tmp (but, contrary to merge --squash, you can choose to replay some, and squashing others).

    所以区别在于:

    • squash 不会触及您的源分支(此处为 tmp)并在您想要的位置创建单个提交.
    • rebase 允许您继续使用相同的源分支(仍然是 tmp):
      • 新基地
      • 更清晰的历史
      • squash does not touch your source branch (tmp here) and creates a single commit where you want.
      • rebase allows you to go on on the same source branch (still tmp) with:
        • a new base
        • a cleaner history

        这篇关于合并 --squash 和变基有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-04 19:24