Hotfixing without branches

Last month, I wrote an email explaining why I don't create branches.

After sending that email, I received this question from a reader (shared with permission):

I'm trying to work at one feature at a time, so I usually don't need feature branches.

There's one thing that's difficult for me, maybe you could tell us something about this in one of your upcoming posts:

How do you deal with hot fixes?

I know, there's keeping features small, commit often and early. But sometimes this isn't possible, and a feature will take e.g. 3 days of work. Now, if there's an urgent bug in production that should be fixed asap, what are you doing?

Git stash probably won't help here, as there might be commits already that would deliver an incomplete feature.

Option 1: Worktrees

Whilst I don't create branches, I do use Git worktrees, which allows me to have multiple versions of the code checked out at the same time - similar to having multiple clones of a repository.

Having multiple worktrees, you don't need to stash the code for your incomplete feature or worry about commits you haven't pushed yet.

You can create a new worktree, fix the urgent bug and switch back to your main worktree when you're finished.

You'll still need to update the original worktree with your new changes which may result in conflicts - the same as merging or rebasing onto a branch.

Option 2: Feature Flags

My preferred approach is to use feature flags, a.k.a. feature toggles.

My wrapping the incomplete feature in a feature flag, it can be deployed but won't be active until the flag is enabled - similar to writing a new Drupal module but not enabling it.

This is a technique I use often as it works well with trunk-based development and continuous integration and delivery.

When the incomplete feature is feature-flagged, you can fix the bug and deploy the hotfix without stashing or rebasing any changes, and you can leave the flag disabled. When the feature is ready, enable the feature flag to activate it and, if you need to turn it off again (maybe that's causing the next bug), you can easily disable it without needing to revert and deploy the code again.

You can just turn the feature flag back off.

I hope that helps!