How to perform rebase (squash) using tortoisegit

Cannot get how to perform squash rebase for the current branch. The console command would be

git rebase -i HEAD~2

and then squash as usually. But how to do the same in TGit?

Script to initialize the case

git init .
touch 1
git add 1
git commit -m "1"
touch 2
git add 2
git commit -m "2"
touch 3
git add 3
git commit -m "3"

As a result after squashing we would have 1 commit with 3 files.

Any proposals?


Solution 1:

This works for me using TortoiseGit 1.7.12:

  1. Right-click on the working directory where you want to do an interactive rebase and choose TortoiseGit -> Show log from the context menu.
  2. In the appearing "Log Messages" dialog, right-click on the most recent commit that you would not like to rebase anymore and choose Rebase master onto this... from the context menu.
  3. In the appearing "Rebase" dialog, tick the Force Rebase checkbox and then right-click on the commit to choose between Pick, Squash, etc., or tick the Squash ALL checkbox in your case.
  4. Press the Start Rebase button, which on success turns into a Commit button, and then into a Done button. Press all of them.

Note that in your example script you would squash / fixup to the root commit, which is a special case and does not work as described above because the root commit has no parent that you could select in step 2.

Solution 2:

You can combine two adjacent commits using the GUI as follows. Remember not do combine commits already on the shared repository. See: Right click 2 commits

Solution 3:

Here's what I did using Tortoise Git & storing the repo on Assembla (a service which competes with GitLab, BitBucket, etc).

I decided I wanted to effectively delete my entire commit history, and start the repo over from scratch. I could have deleted the local git folder and the corresponding repo in Assembla, and then recreated it, but thought it was better to figure out how to do this the “right way”.

So, here’s how I achieved it:

  1. Using Tortoise Git, display the repo’s log. Highlight all the commits, right click on them, and select “combine into one commit”.

  2. In the dialog that pops up, delete the commit comment (which becomes a combination of all the prior comments), and replace it with a single comment e.g. REBASE. Then initiate the commit. The local repo will then be effectively started from scratch (with all the files still added to it of course) without having to delete and recreate it.

  3. Unfortunately, you can’t just push it to Assembla. It will reject this, insisting that your “head” is behind the remote branch. So, to solve that first go to the “Settings” page for the repo in Assembla. Enable “Allow –force push”.

  4. Now perform a “forced push”. I don’t know if Tortoise Git has a gui option for this, but it is easy to do via the command prompt:

    cd [your repo folder] git push -f origin

Done!

UPDATE:

To "force push" with TortoiseGit, in the push dialog there are checkboxes to force "known changes" and "unknown changes". I'm not sure what the difference is, but they both result in using the git --force switch. Try "known changes" to begin with.