Making continuous integration and trunk-based development habits can be a challenge. I’ve never met anyone (myself included) who was able to switch from the habit of weeks-long feature branches to continuous integration over night.
Changing habits like that takes a while. The hardest part, at least in my experience, is learning to think about “large” features more incrementally and interatively. But there are a few tricks I’ve found that can help learn to think in this new way.
One of the most powerful ones is to determine to merge something every day, even if you still have a long-lived feature branch.
I don’t think I’ve ever seen a large feature branch that couldn’t have something cherry-picked out of it, and merged early. And usually when that’s possible, it’s also desirable. First, here’s how it works:
Imagine you have a large feature you’re working on, but it’s not complete. In the process of building your feature, you discover a pre-existing bug, or you rename an existing variable, or maybe you add some simple utility function to help calculate π to exactly 8,324 digits. Any of these could easily be merged into your mainline codebase, without the rest of your feature, and without risk of breaking anything. In fact, in the case of the bug fix or refactor, they almost certainly should be merged first, to reduce the risk if your feature is later reverted.
So here’s what you do (assuming you’re using git):
In your working copy, checkout the latest version of
main
Create a new branch for your cherry-picked bug/refactor/utility
Apply just your single change to the new branch.
Push the new, micro branch up to your central server for review and merge.
Once merged, rebase* your large feature branch on top of the new version of `main. Your large feature branch is now a bit smaller than it was before, because it no longer contains your small change. And what’s more: customers, and other developers, can immediately benefit from that change, and you’ll never have to resolve a merge conflict related to it again!
*If rebasing scares you, it would be good to spend some time learning how to do effective rebasing, as in the long run it’s much less error-prone than merging from main
into your working branch. But that’s a topic for another day.
If you enjoyed this message, subscribe to The Daily Commit to get future messages to your inbox.
Top comments (0)