xxxxxxxxxx
# Reset the current branch to the commit just before the last 12:
git reset --hard HEAD~12
# HEAD@{1} is where the branch was just before the previous command.
# This command sets the state of the index to be as it would just
# after a merge from that commit:
git merge --squash HEAD@{1}
# Commit those squashed changes. The commit message will be helpfully
# prepopulated with the commit messages of all the squashed commits:
git commit
xxxxxxxxxx
We have those commits:
Made something I dont know what with this thing
Fix up some bug of that thing
Start to make that thing
We need to squash it!
# Reset the current branch to the commit just before the last 3:
git reset --hard HEAD~3
# HEAD@{1} is where the branch was just before the previous command.
# This command sets the state of the index to be as it would just
# after a merge from that commit:
git merge --squash HEAD@{1}
# Commit those squashed changes.
git commit -m "Made that thing"
# If you've pushed it, then we need to change remote branch with force
git push --force
How to squash commits on interactive rebase
xxxxxxxxxx
$ git rebase -i HEAD~3
Change number 3 for the number of commits of your project
xxxxxxxxxx
git log --oneline
git rebase -i HEAD~<number of commits to go back and squash>
#change to squash the ones to squash and leave one on pick
xxxxxxxxxx
git reset --soft HEAD~2
git commit -m "new commit message"
git push -f
xxxxxxxxxx
Another way to squash all your commits is to reset the index to master:
git checkout yourBranch
git reset $(git merge-base master $(git branch --show-current))
git add -A
git commit -m "one commit on yourBranch"