During development I often need to commit and push "intermediate" commits, i.e. commits with the code not compilable or in the middle of changing something, etc. I don't want to make such commits, but this is required for easy continuing my work from office to home, sometimes for other developers to get them base on which they can start their work.
I thought I had found a solution for that problem:
I create a separate "dev" branch and make all intermediate commits.
Once the code is in good state, make a merge to master. So the master wouldn't contain "intermediate" commits but only "normal" commits.
Delete the "dev" branch with all intermediate commits.
But this doesn't work. When I make merge, not only the merge commit is included to master, but also all "intermediate" commits from "dev" branch too. So deleting "dev" branch gives nothing, its "intermediate" commits stay there.
So the questions is: is it possible to make merge in the way that master would include only its own commits + merge commit and not include the commits from the second branch? If it's impossible, could you advice me how to achieve my goal - to have the ability to temporary save intermediate results but also seamlessly delete them later?
When you merge your "dev" branch into master, try
git checkout master
git merge --squash dev
git commit -m "Add new feature."
The --squash
option will squash all of your intermediate changes into one big change.
You can also use git rebase --interactive
if you need more finite control (e.g., reordering commits and doing multiple small squashes). This answer explains the differences between git merge --squash
and git rebase --interactive
.
What you want is a "squash" merge:
git checkout master
git merge --squash dev
git commit -m 'current stable work from dev branch'
What this does is create the same state in your working tree as a regular merge, but does not create a merge commit with all the intermediate commits as ancestors. The intermediate commits will not be part of the history of the master
branch. The documentation for this option from the git merge
manpage says:
Produce the working tree and index state as if a real merge happened (except for the merge information), but do not actually make a commit or move the
HEAD
, nor record$GIT_DIR/MERGE_HEAD
to cause the next git commit command to create a merge commit. This allows you to create a single commit on top of the current branch whose effect is the same as merging another branch (or more in case of an octopus).
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