r/AskProgramming • u/GoingOffRoading • Apr 15 '26
Architecture Feeling gas-lit... Branch strategy question
Friendly neighborhood TPM here... Was told something today that threw me off.
We have four environments:
- Production/Main
- Staging
- Integration
- Dev
Code committed to Dev gets promoted to Integration. Then promoted to Staging. Released to Production.
Vanilla right?
I am not in the thick of things, but my understanding is there is crazy merge conflicts between the environments and a variety of people have mismanaged our new code-base and cloud environments.
My Staff Engineer is telling me that normal SOP is to create a feature branch from Prod, then commit to to Dev, then work your way up the pipe. NOT create a feature branch from Dev and work your way up.
Is this normal? Or does this smell like butt covering?
9
u/LWdkw Apr 15 '26
Your staff engineer is right.
17
u/gibdimkoofchji Apr 15 '26 edited Apr 15 '26
Environments should not be branches. If i have to merge my code from an integration testing branch to a production branch, I’m literally never testing the actual code being deployed.
You promote entire branches through the pipeline. Trunk (or main or master whatever you like) becomes dev… when you’re ready for integration testing you create “release-1” from trunk. You keep committing new things to trunk. To pull them in, you cherry pick the commit from trunk to “release-1”. Do that until you’re bored and ready for staging… tag the “release-1” branch (maybe release-1.0!)… awesome.. everything is great in staging.. now you have the exact tag to send to production that’s the exact same code.
If you’re resolving conflicts between integration testing and staging/production, your process is fundamentally flawed
tl;dr what you described is fairly normal, but only because we kind of suck as a profession
2
u/Significant_Long5057 Apr 15 '26
Why do you have branches for each environment?
4
u/LoudAd1396 Apr 15 '26
This approach assumes e very feature moves neatly from dev->int->stage->prod.
What inevitably happens is things get orphaned in dev/int/stage so you can no longer safely move a single feature up the chain.
Its built on good intentions; branching off prod makes good sense. But reality and shifting priorities always fuck up lower environments.
3
u/Sprinkles_Objective Apr 15 '26
The only long term sane git strategy I've seen is trunk based. Main is continuous dev, new features should be a new branch, merged via PR and review, then when you're ready to start a release you create a release branch, test it with some release candidate builds, and if you need to make changes pull those changes in from the main branch via cherry picking. Release branch never merges back, it's just to isolate whats in that release. Usually I will do release branches like release/1.2 and then I'll release 1.2.0, 1.2.1, 1.2.2, etc from that branch, so it's a minor release version and patch versions can be tagged off it. For bigger projects with more complicated releases sometimes I'll make a release branch for a patch version.
This has worked really well. I hate "stage" branches, and I feel like main/master should always be your development branch. Release branches are a huge win because you never have to code freeze or prevent merges worrying about pushing something into release too early, or pushing something in and then having a hard time releasing a hot fix without releasing a bunch of stuff that's not ready to go out. Your release process is never stepping on the dev branch.
7
u/atarivcs Apr 15 '26
When you start to code a new feature, you want to start from the latest stable codebase, i.e. production, right?
So yes, you make a branch off of prod.
2
u/ConcreteExist Apr 16 '26
I would think the only time you would cut a branch off prod is to do an emergency hotfix, and that branch should still make it's way back to the 'lower' branches.
Overall, I think you're inviting a mess by tying branches directly to environments, and it suggests your whole devops implementation is janky to say the least. An ideal set up would allow you to deploy from any branch to, theoretically, any environment on demand.
1
u/northerncodemky Apr 15 '26
If you branch from dev you would be working on top of work that might never see prod, meaning you’d need to unpick it. Branching from ‘prod’ (I assume you really mean main here?) is the correct approach.
1
u/aezart Apr 16 '26
Our process doesn't map branches to environments. We write code in a feature branch then merge it to master, which kicks off a cicd process to build and deploy to Dev. We then migrate that same artifact to QA and then prod with our deployment management tool, which also handles per-environment credentials and other config.
1
u/i8beef Apr 16 '26 edited Apr 16 '26
I've seen tons of variants. A "branch per env" interweaves your git structure with deployment, and isnt good for software products with standard release cycles. You usually see it when people are trying to enable FEATURE deployments, e.g., I want someone to be able to merge to
devto work on stuff in an integration environment, but not promote their feature work on to the next environment until its verified in the lower one, instead of holistic code base monolithic deployments (ie, everyone stages their stuff together for a standard release cycle). I have never seen it work well personally...Git works better for PIPELINE deploys where
devgives everyone somewhere to merge their stuff for the next release cycle together and test (in your Develop environment), and then on some cycle you CUT a release fromdevby creating a newreleasebranch (there are a few variants here: can be a merge tomainfor simple case, can be a long livedreleasebranch where you TAG the individual releases, or could be an actualrelease/x.x.xper release branch). Then that branch gets built and deployed, verified and PROMOTED through Integration, Staging, and Prod (same built binary, importantly).Then there's a few other variants of what to do. You can leave hanging
releasebranches, or you can merge that release tomainafter releasing to Prod, etc. Hotfixes created from the lastreleaseor the lastmainand then merged back to BOTHdevand thereleasebranch before going through the same release pipeline, etc.Creating new feature branches from
mainreally isn't that weird though, that's perfectly fine for all cases here. In the normal feature branch flow it just makes thedevmerge a bit more work sometimes, so I'd normally only use that flow if I KNEW I was making a hotfix personally, normal feature work to be released on normal release cadence, I'd usually branch fromdevfor simplicity... it'll bite you once in a while when you have to retroactively turn it INTO a hotfix (cherry picking and then creative merges), but that SHOULD be, by far, more the exception than the rule.i.e.: Branch per environment that conflate git branches and deployment CAN work... for a while. It doesn't scale well, and any real compliance drive will shoot a hole in it as binary promotion is basically required (ie, building a new binary from a different hash for each env doesn't fly). Don't reinvent the wheel, look up GitFlow or a good trunk based cycle and use it instead.
Here's a hint: If you have to EXPLAIN your git model to a new hire, instead of just pointing them at standard documented flows on gitflow or trunk-based development, you probably are setting yourself up for a bad time...
And if anyone brings up wanting to "stage individual features instead of whole releases"... if you figure it out patent it and sell the shit out of it, otherwise run.
1
u/sublimegeek Apr 16 '26
Think of it this way:
By branching off of “prod”, ideally you want your work to be based off of the most stable working code.
It gives you the best “foundation” of which to fix bugs or add new features.
Then, promote from there.
Remember this one crucial thing: boring code ships.
Also, “covering your ass” is the best way to stay employed. That’s not wrong, that’s the mark of a steely-eyed engineer.
1
u/blueeyedkittens Apr 16 '26
I'm not a big fan having branch per environment. That implies that your code is what gets pushed from environment to environment but its not (or shouldn't be). Your build produces some kind of deployable artifact, and its the artifact that gets deployed into a particular environment. When you promote an artifact from one environment to another, you don't create a new branch and build a new artifact from that branch. If you do, you're essentially promoting a brand new artifact that has technically never been tested.
1
u/FitMatch7966 Apr 22 '26
It makes perfect sense.
Imagine 12 coders all working on separate features. Then you want to promote 4 of them to prod (or staging or whatever)
If all 12 are merged with dev, as they must be to test, then isolating the feature to get to prod is nigh impossible. You’d have to take the entire dev branch!
Often dev is used for testing and not all commits are going to pass testing
1
u/Estpart Apr 15 '26
You're doing gitflow, what you're doing isn't standard practice. As many people have said, there can be various valid/invalid reasons behind this.
0
u/[deleted] Apr 15 '26
[deleted] — view deleted comment
4
u/[deleted] Apr 15 '26
[removed] — view removed comment
22
u/[deleted] Apr 15 '26
[removed] — view removed comment