A common development workflow for us is to checkout branch b
, commit a bunch to it, then squash all those commits into one (still on b
).
However, during the rebase -i
process to squash all the commits, there are frequently conflicts at multiple steps.
I essentially want to alter the branch into one commit that represents the state of the repository at the time of the final commit on b
I've done some searching but I haven't found exactly what I'm looking for. I don't want to merge --squash
because we would like to test the squashed feature branch before merging.
Solution: It is possible to squash multiple commits into single one without rebasing.
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.
Squashing by Interactive Rebase. Git's interactive rebase will list all relevant commits in the default editor. In this case, those are the commits we want to squash. Then we can control each commit and commit message as we want and save the change in the editor.
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.
If you don't need the commit information, then you could just do a soft reset. Then files remain as they were and when you commit, this commit will be on top of the commit you did reset to.
To find the commit to reset to:
git merge-base HEAD BRANCH_YOU_BRANCHED_FROM
Then
git reset --soft COMMIT_HASH
Then re-craft the commit, perhaps:
git commit -am 'This is the new re-created one commit'
This is simlar to the answer from Rasmus but broken down into three steps that should always work:
$ git merge feature1
$ git reset --soft HEAD@{1}
$ git commit -c feature1
Explanation:
I've created a tool specifically for this task:
https://github.com/sheerun/git-squash
For example you can call following to squash all commits from "master" commit up to HEAD, no matter how many conflicts there would be along the way:
git squash master
It's basically what GitHub does when you "squash and merge" pull request.
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