I've been reading a little about --squash
ing commits, but they all seem to be go hand in hand with a --rebase
.
I have a feature branch with a set of commits like this:
(Feature) A --> B --> C --> D --> E --> F --> G
/
(Master) M1 --> M2 --> M3
Suppose I want to merge back to the Master
branch, but I want to clean up the commits on my feature first.
Is it possible to:
OR
Either way, can I do a squash directly on my feature, WITHOUT immidiately initializing a Merge
or Rebase
with it?
If so, how can I do this with Git?
Squash commits directly on feature without rebase or merge.
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.
In my team's workflow we often merge with upstream in the middle of a bunch of commits, and rebasing could get ugly. I've found this helpful to squash all commits that are ahead of master down into one:
# Commit any working changes on branch "mybranchname", then...
git checkout master
git checkout -b mybranchname_temp
git merge --squash mybranchname
git commit -am "Message describing all squashed commits"
git branch -m mybranchname mybranchname_unsquashed
git branch -m mybranchname
# Optional cleanup:
git branch -D mybranchname_unsquashed
# If squashing already-pushed commits...
git push -f
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