594

I'm trying to understand the difference between a squash and a rebase. As I understand it, one performs a squash when doing a rebase.

starball
  • 20,030
  • 7
  • 43
  • 238
GiH
  • 14,006
  • 13
  • 43
  • 56

6 Answers6

581

Merge commits: retains all of the commits in your branch and interleaves them with commits on the base branchenter image description here

Merge Squash: retains the changes but omits the individual commits from history enter image description here

Rebase: This moves the entire feature branch to begin on the tip of the master branch, effectively incorporating all of the new commits in master

enter image description here

More on here


The first two diagrams come from About pull request merges on the GitHub Docs

Puka
  • 1,485
  • 1
  • 14
  • 33
Md Ayub Ali Sarker
  • 10,795
  • 4
  • 24
  • 19
  • 21
    That first diagram looks completely wrong to me. Somehow, commit D has ended up with no parent. – IMSoP May 25 '21 at 20:28
  • 2
    Perfect diagrams and explanation! – XardasLord Sep 17 '21 at 07:23
  • 2
    I don't see the difference between "Merge commit" and "Squash and merge". It seems the master branch looks identical in the final state in both cases. – a06e Apr 12 '22 at 12:39
  • @IMSoP I see what you mean - based on the text below the diagram it looks like the arrow (->) should be a plus (+). Hope this makes sense :-D – dgg Oct 19 '22 at 13:20
  • @dan Even so, the link from C to D doesn't just disappear; the whole point of a merge commit is that both sides of the merge retain a full chain of history, so A, B and C are reachable from *both* parents of F. I think Github are doing their users a real disservice by using a diagram that differs so much from what git is actually modelling. – IMSoP Oct 19 '22 at 13:26
  • @IMSoP that does sound like a rather large discrepancy in process. If I have this correct, the deletion of the link from C to D once F comes into play (getting merged back in) is the source of the issue right? Going to check GH docs for some more info on this. – dgg Oct 20 '22 at 14:20
  • 2
    @dgg The link isn't "deleted", it's just not shown on that very misleading diagram. Commits in git are immutable, so if commit D is really the same commit, it has the same parent, forever. I've raised an issue against the GH docs here: https://github.com/github/docs/issues/21507 – IMSoP Oct 20 '22 at 14:57
  • @IMSoP Will keep an eye on this. Ty for the heads up. – dgg Oct 20 '22 at 20:27
461

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

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

to:

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


# In the following graph, G is c--d--e--f--g squashed together

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

and then deleting tmp branch.


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:

See commit 1d14d0c (24 May 2019) by Vishal Verma (reloadbrain). (Merged by Junio C Hamano -- gitster -- in commit 33f2790, 25 Jul 2019)

merge: refuse --commit with --squash

Previously, when --squash was supplied, 'option_commit' was silently dropped. This could have been surprising to a user who tried to override the no-commit behavior of squash using --commit explicitly.

git/git builtin/merge.c#cmd_merge() now includes:

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

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

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

So the differences are:

  • 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
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • 17
    `G` is `c--d--e--f--g` squashed together? – Wayne Conrad Mar 11 '10 at 19:11
  • 9
    @Wayne: yes, G in those examples represent the `tmp` commits squashed together. – VonC Mar 11 '10 at 19:47
  • Isn't the data in `G` exactly the same as in `g`? So would that mean that `G` is the same as `g` except that it has a different parent? – Alexander Bird May 23 '11 at 14:15
  • 3
    @Th4wn: Since Git reasons with snapshots of a all project, `G` won't represent the same content than `g`, because of changes introduced by `X`. – VonC May 23 '11 at 19:04
  • Note to self: a "**cleaner history**" is important, considering a good Git workflow: http://sandofsky.com/blog/git-workflow.html – VonC Jul 31 '11 at 18:25
  • 2
    @VonC: not sure about that last comment. If you have a `git merge --no-ff temp` instead of `git merge --squash temp`, then you get a messier history, but you can also do things like `git revert e`, much more easily. It's a messy, but honest and pragmatic history, and the main branch still remains fairly clean. – naught101 Dec 13 '12 at 01:19
  • 2
    @naught101 I agree. As explained in http://stackoverflow.com/a/7425751/6309 though, it is also about not breaking `git bisect` or `git blame` when used too often (as in `git pull --no-ff`: http://stackoverflow.com/questions/12798767/fast-forward-when-using-pull-and-no-ff-when-pull/12798995#12798995). There isn't one approach anyway, which is why this article described three (http://stackoverflow.com/questions/9107861/git-merge-testing-branch-final-commit-to-master-branch/13470530#13470530) – VonC Dec 13 '12 at 06:24
  • 1
    Hmm - if say commit `c` involved deleting a file that was added in `a` so also present in `X` then `git merge --squash` would have this file in `G` - see [here](http://stackoverflow.com/a/14343784/281545). So G and g would be different versions of the working tree ! I don't know of other pitfalls but probably `git merge --squash` is not the way to mirror the state of `tmp` on `stable`. See also [this](http://stackoverflow.com/questions/1464642/git-merge-squash-repeatedly#comment23592425_4481621) – Mr_and_Mrs_D May 08 '13 at 18:37
  • 1
    When should I rebase and when should I squash? – Martin Thoma May 06 '16 at 07:07
  • 1
    @MartinThoma not that a rebase can also allow a squash (http://blog.ona.io/general/2016/02/02/squashing-with-git-interactive-rebase.html) More generally, you squash when you do not need the intermediate commits. This is a current practice when accenting a PR (Pull Request): see https://github.com/blog/2141-squash-your-commits – VonC May 06 '16 at 07:13
176

Let's start by the following example:

enter image description here

Now we have 3 options to merge changes of feature branch into master branch:

  1. Merge commits
    Will keep all commits history of the feature branch and move them into the master branch
    Will add extra dummy commit.

  2. Rebase and merge
    Will append all commits history of the feature branch in the front of the master branch
    Will NOT add extra dummy commit.

  3. Squash and merge
    Will group all feature branch commits into one commit then append it in the front of the master branch
    Will add extra dummy commit.

You can find below how the master branch will look after each one of them.

enter image description here

In all cases:
We can safely DELETE the feature branch.

Ahmed Nabil
  • 17,392
  • 11
  • 61
  • 88
  • 3
    can you explain what is dummy commit in 2nd picture ?? I am a beginner in git. – Yusuf Jun 06 '20 at 14:57
  • 4
    @Yusuf, it's just an extra commit which contains both branches updates, it's default commit message = "Megre branch XYZ into master" – Ahmed Nabil Jun 07 '20 at 16:10
  • 1
    For "Squash and merge": there is a commit with all grouped commits plus an "extra dummy commit"? – leticia Jul 27 '20 at 21:10
  • 2
    @leticia the commit with all grouped commits = the "extra dummy commit" itself, as the above graph – Ahmed Nabil Jul 28 '20 at 07:19
  • 5
    Then I would argue that 'squash and merge' will not add an extra dummy commit, but rather will just 'rebase'/append the commit in front of the master branch. Dummy commit in the context you are describing it above is not the same in 1. and 3. as dummy commit in 1 is 'Merge branch XYZ into master which is producing this commit' and dummy commit in 3 is 'Squashed commits into this one commit which is not additional commit produced by merge' – BozanicJosip Feb 17 '21 at 12:21
  • 1
    @ahmednabil88 nice explanation !! in the case of `squash merge` (final 3rd figure above) can we say all commits from the feature branch combined into a single commit and that single commit rebased on top of `master` branch like any other usual rebase merge? – rahulaga-msft Jan 08 '22 at 10:02
  • Great, are there any clues about when to use each? also, could rebasing differ from merge or lead to data loss or conflicts compared to merge? – Ahmed Suror Aug 09 '23 at 22:50
  • It's worth noting that commit ids of f1 and f2 will change when rebasing – Ashrith Kumar Peddi Aug 11 '23 at 14:21
107

Merge squash merges a tree (a sequence of commits) into a single commit. That is, it squashes all changes made in n commits into a single commit.

Rebasing is re-basing, that is, choosing a new base (parent commit) for a tree. Maybe the mercurial term for this is more clear: they call it transplant because it's just that: picking a new ground (parent commit, root) for a tree.

When doing an interactive rebase, you're given the option to either squash, pick, edit or skip the commits you are going to rebase.

Hope that was clear!

Mauricio Scheffer
  • 98,863
  • 23
  • 192
  • 275
  • 14
    When should I rebase and when should I squash? – Martin Thoma May 06 '16 at 07:07
  • 1
    @MartinThoma see: https://lwn.net/Articles/328436/ https://lwn.net/Articles/791284/ – Mauricio Scheffer Jun 29 '20 at 08:13
  • It does not matter which you use but I recommend rebase. Rebase changes the parent node of the feature branch but merge does not and I recommend it because it keeps the commit structure simpler but as a git user, it makes not different. https://stackoverflow.com/questions/2427238/in-git-what-is-the-difference-between-merge-squash-and-rebase#:~:text=Merge%20squash%20merges%20a%20tree,parent%20commit)%20for%20a%20tree. – max Sep 14 '20 at 18:38
2

The way I have learned to understand the value and difference between squash, merge, etc. is to put together this short tutorial for myself.


If you follow this it will explain everything you want to know about squash, merge as well as fast forward, and rebase.


Feature Branches

Say you have Main branch at the Origin, with a history of Commits, A.

enter image description here

You pull from Main, and create a new branch, FeatureA, such that you have commit history:

  • A, B, C, F1, F2, F3 (three commits on FeatureA branch)

enter image description here

You want to push you changes into Origin and merge into Main. Since Main on Origin hasn’t changed you are also able to Fast Forward.

Merge with Fast Forward

Apply all the commit history of FeatureA branch on top of Main’s commit history.

enter image description here

Merge with Squash

An option available whenever your branch has multiple commits is to squash. Squashing combines the history of FeatureA branch into a single commit when merging with Main.

enter image description here

Fast forward isn’t possible if there have been other changes on Main.

If you made changes to the FeatureA branch, meanwhile others had pushed into Main, such that the Origin history of Commits is now: A, B, C

enter image description here

You want to push you changes into Main branch on Origin, and you have a few different options:

  • Merge (without fast forward)
  • Rebase,
  • Stash and Pull.

Merge (aka. Merge without fast-forward)

If you were to fetch a local copy of Origin’s Main branch and merge it into your local FeatureA branch, you would create a commit for that merge, M. You local history on FeatureA would be: A, F1, F2, F3, M, where M is the merge commit.

enter image description here

You could then merge to Main (squashing if you wanted). Note that if you don’t squash, you will introduce a Merge commit into the Main commit history:

  • A, B, C, F1, F2, M.

You could avoid this by squashing your branch when merging to Main, as described above in the Squash section, resulting in: A B C F, like so:

enter image description here

Rebase

Rebase is another option to avoid introducing a merge commit into the history. It essentially takes your changes, and makes it as if you had begun on the latest Main, rather than on an outdated version of Main. For example, after rebasing your Feature branch would branch off from A, B, C, (instead of A, as it was originally). The result:

  • A, B, C, F1, F2, F3

enter image description here

  • A, B, C, F (with squash)

enter image description here

Note, there is no Merge commit, M

The way Git achieves this is to systematically--commit-by-commit--apply the changes from origin’s Main to your branch. The end result is as if you had not started your FeatureA branch in the past based off commit A, but as if you started all your changes on top of the latest Main.

The problem is, in the way this is done. For each commit that has to be applied into your FeatureA branch, git checks for merge conflicts, and you have to resolve them. If there has been more than one change to Main on Origin since your FeatureA branch, you can be resolving merge conflicts over and over again.

Perhaps a stash and pull is easier?

Stash and Pull

Stash your FeatureA, Fetch/Pull an up to date local copy of Main from Origin, and either create a new branch and apply your stash. (Or merge your new Main branch into your branch, and apply your stash. If doing the latter, squash to avoid a merge in the commit history.) Then you can push to Origin / create pull request.

M H
  • 325
  • 3
  • 8
0

Merge and rebase both retain commit history vs squash doesn't. Retaining commit history has a huge impact and I learned it the hard way in the following scenario:

We have master, develop and feature branches. Feature is created off of develop and merged to master upon release. Hotfix branch was created off of master and merged (so develop doesn't know about it). Master was not merged back to develop after release so they got out of sync where merge from master back into develop shows changes and forward merge from develop to master also shows changes (even when nothing in develop has changed).

The cause of this was squashing commits when master is merged back to develop. Doing a regular merge which keeps the commit history, fixed the problem. Merge from develop to master doesn't show any changes now as expected.

HirenP
  • 61
  • 1
  • 6