Just for my own reference:
I do a lot of work on branches, and I like to check them in periodically as I achieve chunks of work. But it’s quite granular – way too granular for the main branch. So I’d like to compress them down into a single revision for whatever feature I am implementing for the main branch.
Here’s how it’s done. Let’s say I’m on branch NSL-1168 (I name my branches for their JIRA id). Everything needs to be clean and checked in.
First, the state of master after the most recent merge of NSL-1168 into it is tagged (to begin using this process, I have to do that manually). Then:
Grab the comments on all my incremental changes and concatenate them:
echo 'NSL-1168 squash merge' `date` > ~/tmp/merge_comment echo >> ~/tmp/merge_comment git log --ancestry-path NSL-1168-last-merge..NSL-1168 >> ~/tmp/merge_comment
Merge master into branch. At this stage, work may need to be done to make the merge work. Hopefully not.
git checkout NSL-1168 git merge master
Squash merge the branch into master. This is the magic bit. We merge and commit with the big long comment detaling all the changes.
git checkout master git merge --squash NSL-1168 git commit -F ~/tmp/merge_comment
Finally, tidy up. The squash merge doesn’t produce an explicit record that the master and my branch were synced, so I merge master into the branch again – a no-op – just to document inside git that the branches are the same at that point.
Finally, I move the ‘last-merge’ tag. All my incremental changes from this point will go into the master as a bundle next time I do this.
git checkout NSL-1168 git merge master git tag -f NSL-1168-last-merge
This leaves the branch one commit ahead of the master, which is ok. We do not merge the branch into the master because not doing that is the whole point of the exercise. Ultimately, the master will look like the branch has never been merged into it. But that’s ok – the squash merges will do the job. This means also that if the JIRA branch is deleted from the repository, then all of those incremental changes will be unreferenced and will disappear. Again – this is ok.