I have a lot of commits that I want to squash together into one commit. Of course I may replace pick
with squash
for every commit, but I have a hundreds commits.
Is there a way to do this automatically?
You can do this fairly easily without git rebase or git merge --squash . In this example, we'll squash the last 3 commits. Both of those methods squash the last three commits into a single new commit in the same way. The soft reset just re-points HEAD to the last commit that you do not want to squash.
The first one is to use the git merge command with the squash flag (two dashes there). And the second one is through an interactive rebase. The first option (merge) is very simple to perform.
If you have a sequence of commits
... - C1 - C2 - C3 - C4 - C5 <- HEAD
and you want to squash C2
to C5
into a single commit, you can reset your branch to C1
while keeping the state of your working directory and staging area, ans then commit again:
git reset --soft C1
git commit
This will require you to re-enter a commit message. You can of course use git log
before resetting and copy the parts of the commit messages you want to keep.
If you want to squash a feature branch into a single commit ontop of the master
branch, another option is to use the --squash
option to git merge
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With