46

Is it possible to squash in SourceTree? I saw that apparently you can "drag and drop" commits in order to squash them. However, when I do that it only highlights several commits.

Miles
  • 553
  • 1
  • 6
  • 8

7 Answers7

37

Updated Answer

SourceTree for Windows

As of version 1.5, you can now do interactive rebases, which will allow you to squash.

SourceTree for Mac

Interactive rebase has been available in SourceTree for Mac since version 1.6 (emphasis mine):

The git rebase –interactive command allows you to reorganise your commits after you’ve created them (but before you’ve made them public), and now you can do this inside SourceTree. Combine (squash) multiple commits together, or re-order them, simply by dragging & dropping. You can also change the commit message, or edit the content of the commits. Just right-click on a commit in the log and choose ‘Rebase children of <sha> interactively’ to kick the process off.

Old Answer

Apparently squashing commits is a feature in SourceTree version 1.6 for the Mac.

However, the feature doesn't seem to be available in the Windows version of SourceTree, which is currently still at version 1.0.8.

Using the command line

You still have the option of using the command line to squash commits:

git rebase -i <sha-of-base-commit>

In the TODO list, put an s (for squash) next to commits to squash into the previous commit:

pick e953225 Add meow meow meow
s def892d Add master

To learn more about how to squash commits using the command line, see Squashing Commits, from the FREE online Pro Git book.

  • Disclaimer: it may only work OK if you try it _" before you’ve made them public"_. I already had pushed the previous change to the `remote`, then reverted it locally and got an error when trying `push` it to the `remote`. Since I didn't need the old change, I fixed using CLI `git push --force origin e3f1e37:master` from [/git-how-to-reset-origin-master-to-a-commit](http://stackoverflow.com/questions/17667023/git-how-to-reset-origin-master-to-a-commit/25390763#25390763) – Ricardo Jan 15 '16 at 00:08
  • 1
    @Ricardo - You should be extremely careful force-pushing and changing already-public history, though, since you run the risk of causing others who are working on top of your old commits to lose their work. – JimmidyJoo Mar 16 '17 at 18:01
8

Right-click on the parent commit and select 'Rebase children of <sha> interactively'. After that, you'll be able to drag and drop to squash commits.

https://community.atlassian.com/t5/Sourcetree-questions/In-SourceTree-how-do-I-squash-commits/qaq-p/345666#M10317

Pang
  • 9,564
  • 146
  • 81
  • 122
alvarodoune
  • 921
  • 10
  • 13
7

To squash your latest commits ,if you want to last 10 commits

1) git reset HEAD~10
2) git add .
3) git commit -am "single commit message for 10 last changes"
4) git push --force
sasikumar
  • 12,540
  • 3
  • 28
  • 48
6

As of 1.4.1.0, Windows users still don't have squash. However, you can get the same result by hand.

To squash branch A into branch B:

  1. Create a branch from A and call it C.
  2. Reset C to B in soft mode.
  3. Check out B.
  4. Commit.

    1.              2.                          3.                             4.
        * <- [A][C]    * <- [A]                    * <- [A]                       * <- [A]
        |              |                           |                              |
        *              * o <- [C] with changes     * o <- [C][B] with changes     * * <- [C][B] committed
        |              |/                          |/                             |/
        * <- [B]       * <- [B]                    *                              *
        |              |                           |                              |
        :              :                           :                              :
    

You don't need to create C if you won't need to keep the position of A.

I recommend opening gitk aside while doing these because you can see all detached commits until you close. (I'd like to see this generosity in SourceTree)

At least, reflog is your friend.

snipsnipsnip
  • 2,268
  • 2
  • 33
  • 34
  • Even if I don't keep the original branch I would create a new branch, just in case I mess it up. For example I could forget to select "soft" mode. Or make sure the original branch was safely pushed somewhere. – Florian F Oct 28 '21 at 06:59
2

Old school Squashing within your own local feature branch

This answer works with most versions of Sourcetree on any platform, because I am using old functionality only. It is quite simple, after you think of it. As usual, it only works before making your work public. I use it to create "tidy" commits in the shared repository, while committing frequently in my own local repository. Assume you are in local branch myfeature. Suppose you are several commits ahead of branch origin/myfeature. You can stay within branch myfeature throughout the procedure, so you will not influence anyone else's work.

Step 1

Do a "reset current branch to this commit" on the latest published commit. That is the commit labeled origin/myfeature. A mixed reset will keep all your changes. Be sure to check that your reset is mixed. (If you inadvertedly do a hard reset, you will lose your work...)

Step 2

Stage your changes (as usual). Don't forget the files you added or deleted on the way!

Step 3

Commit. Write a good commit message that summarizes all of your own (local) commit messages, because they are lost.

Done!

You have squashed everything and cleaned up your local commits in step 1. You are still on your own feature branch, so you haven't influenced anything else. If you feel insecure, label your latest commit before doing anything else. Just for safe keeping. In that way you can always hard-reset to that label in case things go wrong. If everything works out, just remove that label to clean up your commit-history.

Stef Joosten
  • 337
  • 1
  • 8
0

tl;dr You have to select the commit from where you want to start and rebase interactivly (as squashing is a git option of rebase (and merge).

Select the commit, for example ab12c3, from which you want to add future commits as a squash. Right-click -> Rebase children of ab12c3 interactively...

In the new dialog,

  1. reorder commits if needed
  2. then select each commit (not the check mark! but select the line)
  3. then Squash with previous and in the end
  4. edit message of the squashed commit(s).

Additional info: You'll end up with all the commits and squashed commits rebased on commit ab12c3. This might differ from what you've already pushed to remotes like origin. As the following guide tells, you need to push with force push activated.

Perfect guide (with screenshots) found here: https://www.linkedin.com/pulse/how-squash-commit-using-source-tree-ardall-leonardo

PythoNic
  • 303
  • 5
  • 13
-7

They just added the support for "squash" in Windows version as well.

SourceTree version 1.2 for Windows

mjlee
  • 3,374
  • 4
  • 27
  • 22