xxxxxxxxxx
There are two options to squash the last N commits into a single commit include both of the below-mentioned options in your answer
If you want to write the new commit message from scratch use the following command
git reset –soft HEAD~N &&git commit
If you want to start editing the new commit message with a concatenation of the existing commit messages then you need to extract those messages and pass them to Git commit for that I will use
git reset –soft HEAD~N &&git commit –edit -m”$(git log –format=%B –reverse .HEAD@{N})”
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
git rebase -i HEAD~5
# As the commit on line 1 is HEAD, in most cases you would leave this as
# pick. You cannot use squash or fixup as there is no other commit to
# squash the commit into.