To squash all commits in your current branch into one commit, follow these steps:
-
First, identify how many commits you want to squash by checking your Git history:
git log
-
Start an interactive rebase going back to the appropriate number of commits. Replace
N
with the number of commits you want to squash:git rebase -i HEAD~N
Or, if you want to squash all commits since branching from
main
:git rebase -i main
-
Your text editor will open, showing a list of commits. Change all commits except the first one from "pick" to "squash" or just "s":
pick abc1234 First commit message s def5678 Second commit message s ghi9101 Third commit message
-
Save and close the editor. Another editor will open, allowing you to edit the combined commit message.
-
After editing the commit message, save and close the editor to complete the squash.
-
If you've already pushed your branch, you'll need to force push:
git push --force-with-lease origin your-branch-name