-
Notifications
You must be signed in to change notification settings - Fork 22
Squashing Commits
If you find yourself on this page, someone has probably told you to "squash your commits."
All this means is that you have a lot of commits for a task that could probably have been accomplished by one commit. In order to squish all your commits into one commit, start with the following:
git log --format=oneline
This will print the commit history with the hash ID of each commit. Find the hash of the commit you would like to squish all the ones above it into; if the log is long, you may need to hit 'q' to exit. Then:
git rebase -i abc1234
This runs git in "interactive rebase" mode. It will open up a text editor listing all the commits up until the commit with ID abc1234
, and explains some instructions in the comments. Put a "p" or "pick" in front of the commit you would like to keep and an "s" in front of all the commits you want to squash into it.
Save the file. We have now rewritten history! That's kind of scary. You may want to check with git log
and git diff HEAD^
to make sure you did your rebase properly. Once you are confident that everything is in good order, we will forcefully rewrite our history on the remote side by running:
git push --force origin HEAD
We must use the --force
flag, since git will not permit us to rewrite history without telling it that we're really sure about it.