Another way to squash all your commits is to reset the index to master:
1 | git checkout yourBranch |
This isn’t perfect as it implies you know from which branch “yourBranch” is coming from.
Note: finding that origin branch isn’t easy/possible with Git (the visual way is often the easiest, as seen here).
Note: git branch --show-current has been introduced with Git 2.22 (Q1 20219).
EDIT: you will need to use git push --force
Karlotcha Hoa adds in the comments: For the reset, you can do
1 | git reset $(git merge-base master $(git rev-parse --abbrev-ref HEAD)) |
[That] automatically uses the branch you are currently on.
And if you use that, you can also use an alias, as the command doesn’t rely on the branch name.
sschoof adds in the comments:
Since my default branch is called main and my search had multi times brought me here:
To copy it for my next time
1 | git reset $(git merge-base main $(git rev-parse --abbrev-ref HEAD)) |