I'm looking for a way to squash all git commits into a single big commit in master
branch. I fully understand the consequences of what I'm trying to do, no need to explain that this is dangerous or that it's not the right way to go - I want to lose all my history and turn this repository into a single big commit.
The main problem is: I have no other living branches, no local commits, and all of the previous commits have already been pushed to remote master
.
Hacky scripts are also welcome.
Squashing by Merging With the –squash Option This can effectively clean the commit-graph in a branch. However, we sometimes make many commits in our feature branch while working on it. After we've developed the feature, we usually want to merge the feature branch to the main branch, say “master”.
To "squash" in Git means to combine multiple commits into one. You can do this at any point in time (by using Git's "Interactive Rebase" feature), though it is most often done when merging branches. Please note that there is no such thing as a stand-alone git squash command.
Squash merging is a merge option that allows you to condense the Git history of topic branches when you complete a pull request. Instead of each commit on the topic branch being added to the history of the default branch, a squash merge adds all the file changes to a single new commit on the default branch.
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.
I would use git reset --soft
:
git reset --soft id-of-first-revision-of-master
git commit --amend -m "single commit for master"
Then you can git push --force
wherever you need that new branch.
I can think of another simple way to do it, with more git commands:
git checkout --orphan some-branch
git commit -m "First commit"
git branch -f master # move the local branch
git checkout master
git branch -d some-branch # delete the temp branch
It could also be done like this, in a more hackish fashion:
git commit-tree -m "First commit" HEAD^{tree}
That will write a revision.... if you check it out, you will have a single revision, content will be exactly like it is where you were standing before.... feel free to move the local branch (as explained in the previous recipe)
You can still modify the history on the upstream by using git push --force-with-lease
. However you have to be aware of the consequences.
Using git push --force
will create a parallel tree on your upstream so all the developers may find themselves lost in a legacy branch.
In order to squash your history, simply do:
git rebase -i HEAD~10
Where 10
is the number + 1 of commits you want to squash together. If you want to squash all the commits, then just refer your <first-commit-hash>
instead of HEAD~10
. Then on the editor you select squash
for all the commits you want to group together. You can do search/replace: pick
by squash
Once done, simply push your changes:
git push --force-with-lease
I would never recommend to do --force
because if another developer has pushed a commit in the meantime you will erase its move. By using --force-with-lease
Git will prevent you to push if somebody else has pushed on the top of your last change (see this question for more details).
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