Git Extensions: Squash Commits?
To Squash Multiple Commits, I Have Always Used: Git Reset --Soft Head~ && Git Commit but I Wonder If There Is a Good Way to Do This in a Good Git Client Like...
To squash multiple commits, I have always used:
git reset --soft HEAD~<number of commits to squash> && git commit
But I wonder if there is a good way to do this in a good git client like git extensions? It would be cool if you could just select consecutive commits and squash them.
4 Answers
There are multiple ways of doing a squash.
Here's how you can easily squash the current and all its immediate parent commits into a single commit in Git Extensions:
- Right click on a commit you wish to squash to and select "Reset the current branch to here"
- Select either "Soft reset" (retain staged files) or "Mixed reset" (unstage all files)
- Stage, if necessary
- Commit
Here's an animation of the above:
Another way is to do an "interactive rebase" either
- via a command line (
git rebase -i, read docs), or - via UI (e.g. Git Extensions).
To do an interactive rebase in Git Extensions:
- Right click on a commit you wish to squash to and select "Rebase current branch on > Selected commit interactively..."
- In the presented dialog alter the history, such as choose which commits to squash or reword
- Save and close
Here's an animation of the above:
Here is the slowed down version of the accepted answer
(Assuming you're using Windows)
Yes, I believe TortoiseGit can do it. As a previous user of TortoiseSVN, I would recommend it. When viewing the commit log, you can do the following:
In addition, when you commit using TortoiseGit, you'll have the option to simply amend your previous commit, so you can do this as you go. It will also pull in the last commit message when you do this (I cleared mine for privacy reasons in the screenshot).
Of course, I'm sure you already know this, but don't try to combine or amend commits that are already pushed to remote, or your next push will fail miserably.
As an extra bonus, you'll get the benefits of overlay icons when browsing your working copy in Explorer.
You could achieve this easily with GitExtensions by 2 ways.
The first is to do exactly the same thing but from the GUI which is suitable when you don't want to use the existing commit messages of the commits you watch to squash. .
git reset --soft HEAD~<number of commits to squash>
From the history browser of GitExtensions, right click on the last commit you want to keep and select 'reset branch to here'. Then select the option 'soft'.
git commit
Then commit the changes that are still staged.
The second is to a 'rebase --interactive' :
Right click on the same commit described above and select 'rebase on'. In the pop-up check the checkbox 'interactive'.
Then, in the editor, set the commit action to 'squash'. Read a good documentation on the interactive rebase before doing it. This solution is perfect when you want to write a good commit message built from the commits messages from the commits you are squashing.