How to squash commit using Source Tree
As one of git best practices, Commit early, and often, sometimes the changes contain tons of commits. Nothing wrong with it, but there are certain cases where a clean and semantic commit message is required where one single commit is preferable over multiple commits. That's where squashing the commit comes in handy. Squashing a commit is combining the commit into one from the last selected commit until your last commit.
Please do the following:
- Open your source tree and view it in history mode.
- Change the option to the current branch so it's easier to see your particular commits.
- Spot the last commit you want to squash and then right-click on one commit before it.
- Choose to rebase children of commit_hash interactively.
- Choose to rebase children of commit_hash interactively.
- A pop up will appear, just focus one 3 sections (1) List of commits, (2) Squash with the previous button, (3) OK button.
- In section 1 you can squash, re-order the commit, or selectively combining it. But if you just want to combine all, just select from the top and keep hitting the Squash with the previous button until it becomes one single commit.
- After that, you can right-click and edit the commit message accordingly.
- Click OK.
- The last step is to push these changes to the remote branch, but Source Tree will keep telling you that you're behind and need to pull from remote. It's expected cause the local HEAD is behind due to git rebase that happens when you all do these steps. You need to force the push by typing this command in the terminal.
git push --force origin your_branch
You may want to refer to this semantic commit message for a better commit message: https://seesparkbox.com/foundry/semantic_commit_messages
Hope it helps!
This was very helpful , Thank you so much
Super helpful!! Thank you!
To allow force push on Mac you can go to SourceTree -> Preferences -> Advanced -> Allow force push
You can force push in Sourcetree if you first enable it: Tools -> Options -> Git -> Enable Force Push 😉
where execute in sourcetree? git push --force origin your_branch