GitLab Flow: Environment and Release-Based Branching

Master GitLab Flow — the branching strategy that combines Git Flow simplicity with deployment pipelines. Learn environment-based and release-based branching patterns.

published: reading time: 20 min read author: Geek Workbench updated: March 31, 2026
Quick Summary

GitLab Flow is used for several related branching patterns, including short-lived feature branches, environment branches, and long-lived release branches. This guide focuses on environment and release branches, while separating Git history from artifact promotion, deployment approvals, and environment protection. Choose the pattern that fits the product’s release and support model.

GitLab Flow: Environment and Release-Based Branching

A team may need separate validation environments or support for multiple customer versions. GitLab Flow is a name used for several related workflows, so the right branch layout depends on those deployment and support needs.

The branch pattern can add structure around review, staging, or versioned releases. A branch name alone does not create a safe deployment gate; configure artifact promotion, approvals, and environment access as separate controls.

There is no single required GitLab Flow layout. GitLab’s branching strategy guide describes web-service, long-lived release-branch, and branch-per-environment patterns. The examples below focus on environment branches and release branches; short-lived issue or feature branches can feed either pattern.

When to Use / When Not to Use

Use GitLab Flow When

  • Staging environment required — You deploy to staging before production for validation
  • Multiple deployment targets — Branches can map to environments, though the mapping does not itself validate or protect a deployment
  • Scheduled releases with CI/CD — You want release branches but with automated pipelines
  • GitLab users — Native integration with GitLab CI/CD and environment management
  • Controlled releases — You want separate branch references alongside configured review and deployment controls

Do Not Use GitLab Flow When

  • Short-lived branch workflow — If short-lived feature branches and your configured deployment controls meet your needs, GitHub Flow may use less branch overhead
  • No staging environment — If you only have production, environment branches add no value
  • Complex release management — If you need hotfixes, support branches, and version maintenance, Git Flow may be more appropriate
  • Small teams — The environment branch overhead may not be justified

Core Concepts

The examples below cover two patterns. Teams can combine them with short-lived issue or feature branches, or choose a different flow if the product has simpler deployment needs.

Environment-Based Branching

Branch Purpose Deployment Target
main Development; may trigger staging when configured Staging environment
production Ref selected for production promotion Production environment

In this example, a merge to main can start a staging deployment if the pipeline is configured to do so. A later merge to production changes the branch ref; it does not prove that staging and production use the same build. Prefer building once, recording the source SHA and artifact digest, then promoting that artifact. Merge approvals and protected-environment deployment approvals are separate controls.


graph LR
    A[feature branch] -->|merge| B[main - Staging]
    B -->|merge| C[production - Live]
    B -->|if configured| D[Deploy artifact to Staging]
    C -->|after configured approval| E[Promote same artifact to Production]

Release-Based Branching

Branch Purpose Deployment Target
main Active development Pre-release / Nightly
release/X.Y Stable release branch Production

This pattern keeps main moving while a release branch receives fixes for a supported version. The release cadence and merge-back policy are team choices.


graph LR
    A[feature] -->|merge| B[main]
    B -->|cut| C[release/1.0]
    B -->|cut| D[release/2.0]
    C --> E[Production 1.0]
    D --> F[Production 2.0]
    C -->|cherry-pick| G[release/1.0.1]

Architecture and Flow Diagram

One possible environment-branch workflow is shown here. Pipeline rules determine which events run jobs; protected environments and approval rules determine who can deploy. Where possible, build an artifact once and promote that same digest rather than rebuilding after each branch merge.


graph TD
    A[Developer] -->|push| B[feature branch]
    B -->|MR + CI| C{CI Pass?}
    C -->|No| D[Fix and retry]
    D --> B
    C -->|Yes| E[Merge to main]
    E --> F[Deploy to Staging]
    F --> G[QA Validation]
    G -->|Approved| H[Merge main to production]
    H --> I[Deploy to Production]
    H --> J[Create Release Tag]

Step-by-Step Guide

1. Environment-Based Setup

Start with the simplest variant — two branches mapping to two environments:


# Initialize repository with main branch
git checkout -b main
git push -u origin main

# Create production branch
git checkout -b production
git push -u origin production

# Set up branch protection
# main: requires MR approval, CI must pass
# production: allow approved MR updates from main only
# Separately protect the production environment and configure required deploy approval

2. Feature Development

Features branch from main and merge back through merge requests:


# Create feature branch
git checkout main
git checkout -b feat/user-dashboard

# Develop and push
git add .
git commit -m "feat: add user dashboard with analytics"
git push -u origin feat/user-dashboard

# Open merge request
gitlab mr create \
  --source-branch feat/user-dashboard \
  --target-branch main \
  --title "Add user dashboard" \
  --description "Implements the user dashboard with analytics widgets"

3. Deploy to Staging

In this example, the pipeline rules below start staging deployment for updates to main:

# .gitlab-ci.yml
stages:
  - test
  - build
  - deploy-staging
  - deploy-production

test:
  stage: test
  script:
    - npm ci
    - npm run lint
    - npm test
  rules:
    - if: '$CI_PIPELINE_SOURCE == "merge_request_event"'

build:
  stage: build
  script:
    - npm ci
    - npm run build
  artifacts:
    paths:
      - dist/
  rules:
    - if: '$CI_COMMIT_BRANCH == "main"'

deploy-staging:
  stage: deploy-staging
  script:
    - ./scripts/deploy.sh staging
  environment:
    name: staging
    url: https://staging.example.com
  rules:
    - if: '$CI_COMMIT_BRANCH == "main"'

4. Promote to Production

When staging is validated, merge main into production:


# Create merge request from main to production
git checkout production
git merge main
git push origin production

# Or via GitLab CLI
gitlab mr create \
  --source-branch main \
  --target-branch production \
  --title "Promote to production" \
  --description "Staging validated. Ready for production deployment."

5. Release-Based Variant

For teams that need versioned releases:


# Cut a release branch from main
git checkout main
git checkout -b release/2.0
git push -u origin release/2.0

# Deploy release branch to production
# .gitlab-ci.yml deploys release/* branches to production

# Hotfix for release
git checkout release/2.0
git checkout -b hotfix/2.0.1
# Fix the bug
git commit -am "fix: resolve payment processing error"
git checkout release/2.0
git merge hotfix/2.0.1
git push origin release/2.0

# Merge hotfix back to main
git checkout main
git merge release/2.0
git push origin main

Production Failure Scenarios

Scenario What Happens Mitigation
Staging passes, production fails Environment difference causes production-only bug Use identical infrastructure for staging and production; run smoke tests post-deploy
Production branch diverges Hotfixes to production aren’t merged back to main Enforce merge-back policy; CI checks that production is an ancestor of main
Release branch staleness Old release branches accumulate without cleanup Archive release branches after 2 versions; automate branch lifecycle management
Merge conflict during promotion main to production merge has conflicts Resolve conflicts in main first; never resolve in production
Pipeline bottleneck Long CI pipelines delay staging deployments Parallelize test suites; use incremental builds; cache dependencies

Trade-off Analysis

Aspect Advantage Disadvantage
Environment mapping Clear branch-to-environment relationship Extra merge step for production
Simplicity Fewer branch types than Git Flow More complex than GitHub Flow
Staging validation Dedicated environment for QA Requires maintaining staging infrastructure
Release management Release branches for versioned software Manual branch cutting and maintenance
CI/CD integration Native GitLab CI/CD pipeline support Less portable to other CI systems
Audit trail Clear promotion path from staging to production Additional merge commits in history

Implementation Snippets

GitLab CI/CD — Full Pipeline

# .gitlab-ci.yml
stages:
  - test
  - build
  - deploy-staging
  - deploy-production
  - verify

variables:
  NODE_VERSION: "20"

.test-template: &test-config
  image: node:${NODE_VERSION}
  cache:
    key: ${CI_COMMIT_REF_SLUG}
    paths:
      - node_modules/
  before_script:
    - npm ci

unit-test:
  <<: *test-config
  stage: test
  script:
    - npm run test:unit
  rules:
    - if: '$CI_PIPELINE_SOURCE == "merge_request_event"'
    - if: '$CI_COMMIT_BRANCH == "main"'

integration-test:
  <<: *test-config
  stage: test
  script:
    - npm run test:integration
  rules:
    - if: '$CI_COMMIT_BRANCH == "main"'

build:
  <<: *test-config
  stage: build
  script:
    - npm run build
  artifacts:
    paths:
      - dist/
    expire_in: 1 week
  rules:
    - if: '$CI_COMMIT_BRANCH == "main"'
    - if: '$CI_COMMIT_BRANCH =~ /^release\/.*/'

deploy-staging:
  stage: deploy-staging
  script:
    - ./scripts/deploy.sh staging
  environment:
    name: staging
    url: https://staging.example.com
  rules:
    - if: '$CI_COMMIT_BRANCH == "main"'

deploy-production:
  stage: deploy-production
  script:
    - ./scripts/deploy.sh production
  environment:
    name: production
    url: https://example.com
  rules:
    - if: '$CI_COMMIT_BRANCH == "production"'
  when: manual

verify-production:
  stage: verify
  script:
    - ./scripts/smoke-test.sh https://example.com
  rules:
    - if: '$CI_COMMIT_BRANCH == "production"'

Branch Protection via GitLab API


# Protect main branch
curl --request POST --header "PRIVATE-TOKEN: $GITLAB_TOKEN" \
  "https://gitlab.example.com/api/v4/projects/$PROJECT_ID/protected_branches/main" \
  --data "allowed_to_push[]=access_level:40" \
  --data "allowed_to_merge[]=access_level:30" \
  --data "allowed_to_push[]=access_level:30"

# Protect production branch (only merges from main)
curl --request POST --header "PRIVATE-TOKEN: $GITLAB_TOKEN" \
  "https://gitlab.example.com/api/v4/projects/$PROJECT_ID/protected_branches/production" \
  --data "allowed_to_push[]=access_level:40" \
  --data "allowed_to_merge[]=access_level:40"

Environment Promotion Script


#!/bin/bash
# scripts/promote-to-production.sh
set -euo pipefail

echo "Promoting main to production..."

# Verify staging is healthy
STAGING_STATUS=$(curl -s -o /dev/null -w "%{http_code}" https://staging.example.com/health)
if [ "$STAGING_STATUS" != "200" ]; then
  echo "ERROR: Staging health check failed (HTTP $STAGING_STATUS)"
  exit 1
fi

# Create merge request
MR_URL=$(gitlab mr create \
  --source-branch main \
  --target-branch production \
  --title "Promote $(git rev-parse --short HEAD) to production" \
  --description "Staging validated. Commit: $(git log -1 --format='%h %s')" \
  --json | jq -r '.web_url')

echo "Merge request created: $MR_URL"
echo "Awaiting approval for production deployment."

Observability Checklist

  • Logs: Log every environment deployment with commit SHA, branch, and deployer identity
  • Metrics: Track staging-to-production promotion time, deployment success rate per environment, and pipeline duration
  • Traces: Trace each production deployment through staging validation to the original merge request
  • Alerts: Alert when staging is unhealthy for > 1 hour, production deployment fails, or promotion queue exceeds 24 hours
  • Dashboards: Display environment health, deployment frequency, promotion success rate, and pipeline lead time

Security and Compliance: GitLab Flow

  • Protect refs and environments separately: A protected branch controls who can update a ref; a protected environment or manual approval controls deployment access
  • Promote the recorded artifact: Record source SHA and artifact digest, then promote that same artifact between environments
  • Keep deployment records: Merge request history records code review, while deployment records should identify artifact, environment, approver, and result
  • Secret management: Use GitLab CI/CD variables with environment scoping for environment-specific secrets
  • Compliance: Map each requirement to an explicit review, permission, approval, evidence, and rollback control; branch layout alone does not provide those controls

Environment-Based CI/CD Pipeline

# .gitlab-ci.yml — Full environment promotion pipeline
stages:
  - test
  - build
  - deploy-staging
  - deploy-production
  - verify

variables:
  NODE_VERSION: "20"

.test-template: &test-config
  image: node:${NODE_VERSION}
  cache:
    key: ${CI_COMMIT_REF_SLUG}
    paths:
      - node_modules/
  before_script:
    - npm ci

unit-test:
  <<: *test-config
  stage: test
  script:
    - npm run test:unit
  rules:
    - if: '$CI_PIPELINE_SOURCE == "merge_request_event"'
    - if: '$CI_COMMIT_BRANCH == "main"'

integration-test:
  <<: *test-config
  stage: test
  script:
    - npm run test:integration
  rules:
    - if: '$CI_COMMIT_BRANCH == "main"'

build:
  <<: *test-config
  stage: build
  script:
    - npm run build
  artifacts:
    paths:
      - dist/
    expire_in: 1 week
  rules:
    - if: '$CI_COMMIT_BRANCH == "main"'
    - if: '$CI_COMMIT_BRANCH =~ /^release\/.*/'

deploy-staging:
  stage: deploy-staging
  script:
    - ./scripts/deploy.sh staging
  environment:
    name: staging
    url: https://staging.example.com
  rules:
    - if: '$CI_COMMIT_BRANCH == "main"'

deploy-production:
  stage: deploy-production
  script:
    - ./scripts/deploy.sh production
  environment:
    name: production
    url: https://example.com
  rules:
    - if: '$CI_COMMIT_BRANCH == "production"'
  when: manual

verify-production:
  stage: verify
  script:
    - ./scripts/smoke-test.sh https://example.com
  rules:
    - if: '$CI_COMMIT_BRANCH == "production"'

Common Pitfalls / Anti-Patterns

  1. The Staging Quagmire — Staging becomes a dumping ground for half-tested features. Only merge to main when features are complete and tested.
  2. Production Branch Divergence — Hotfixes applied directly to production without merging back to main cause the bug to reappear. Always merge back.
  3. Manual Deployment Drift — Manual deployments outside the pipeline create inconsistency. All deployments must go through CI/CD.
  4. Environment Configuration Drift — Staging and production configurations diverge over time. Use infrastructure as code to keep them identical.
  5. Release Branch Proliferation — Too many active release branches create confusion. Limit to 2-3 active releases maximum.
  6. Skipping Staging Validation — Auto-promoting from staging to production without validation defeats the purpose. Require explicit approval.
  7. Pipeline Coupling — Tying deployment to pipeline success without manual gates for production is risky. Use when: manual for production.

Quick Recap Checklist

  • If this is your deployment policy, configure main updates to deploy to staging
  • Protect the production branch so only reviewed promotion changes can update it
  • Every merge request requires CI validation
  • Production deployments require manual approval
  • Hotfixes merge back to both production and main
  • Environment-specific secrets are scoped correctly
  • Staging and production infrastructure are identical
  • Deployment logs include commit SHA and deployer identity
  • Release branches are archived after 2 versions
  • Pipeline stages are parallelized for speed

Interview Questions

1. What is the difference between environment-based and release-based GitLab Flow?

Environment-based branching maps branches to deployment environments: main deploys to staging, production deploys to production. Code flows downstream from main through merge. This is ideal for web applications with continuous deployment.

Release-based branching maps branches to software versions: main is cutting-edge development, release/X.Y branches are stable versions deployed to production. This is ideal for versioned software with scheduled releases.

2. How do you handle hotfixes in GitLab Flow?

In environment-based GitLab Flow, hotfixes branch from production, fix the issue, merge back to production, and then merge to main to prevent regression.

In release-based GitLab Flow, hotfixes branch from the specific release/X.Y branch, merge back to that release, and then merge to main. This ensures the fix reaches both the current production version and future development.

3. When might you require manual approval for production deployment?

GitLab Flow does not require manual production approval. A team may add a manual job, protected environment, or required approver when its deployment policy calls for one. Those controls are configured in CI and the hosting platform; naming a branch production does not create an approval gate.

For production deployments, build the artifact once, record its source commit and digest, and promote that same artifact after any required checks and approval.

4. How does GitLab Flow compare to Git Flow?

Git Flow describes a specific set of branches for scheduled releases and parallel support. GitLab Flow is a family of patterns, including issue branches, long-lived release branches, and environment branches. The right choice depends on release cadence and support needs.

Either workflow can use CI/CD. A branch may trigger a pipeline, but branch names alone do not prove which artifact was deployed or provide environment protection and approval.

5. What are the main differences between GitHub Flow and GitLab Flow?

GitHub Flow usually keeps work on short-lived branches and merges reviewed changes into main. Whether a merge deploys, and whether that deployment needs approval, depends on repository and deployment settings.

GitLab Flow covers several patterns. Some teams use environment branches to record desired code per environment; others use release branches for versioned support. In either case, artifact promotion, environment protection, and approvals are separate controls. See the GitHub Flow guide.

6. How does GitLab Flow handle environment-specific secrets and configuration?

GitLab CI/CD uses environment-scoped variables. Secrets are stored as CI/CD variables with environment scope, ensuring that staging credentials never reach the production environment. The environment: block in job definitions binds jobs to specific deployment targets, and GitLab's environment dashboard provides a live view of what is deployed where.

7. What happens when a merge conflict occurs during promotion from main to production?

When conflicts occur during the main to production merge, you resolve them in main first, never in the production branch. The fix involves checking out a feature branch from the current main, merging production into it to create a conflict resolution commit, then merging that resolved branch back into main. Once main is conflict-free and re-validated, the promotion merge to production proceeds cleanly.

8. How do you prevent production branch divergence in GitLab Flow?

Production divergence is prevented through a merge-back policy enforced by CI. Every hotfix merged to production must also be merged to main before the pipeline can complete on production. A CI job can check that main is an ancestor of production using git merge-base --is-ancestor. If the hotfix was not merged back, the pipeline fails and blocks the production deployment.

9. Why does GitLab Flow recommend identical staging and production infrastructure?

When staging and production differ, bugs that only appear in production slip through staging validation. This is the "staging passes, production fails" problem. Identical infrastructure—ideally managed through infrastructure as code (Terraform, Pulumi)—ensures that the staging environment exercises the same configurations, service versions, and network paths as production. Any difference is a potential production-only bug hiding in plain sight.

10. How does GitLab Flow integrate with GitLab's environment dashboard?

Each CI job that deploys can declare an environment: block with a name and url. GitLab automatically tracks these deployments in its environment dashboard, showing which commits are currently deployed to each environment, the deployment history, and the health status. When a merge request promotes code between environments, GitLab links the merge request to both the source and target environment deployments, creating a complete traceability trail.

11. What is the role of branch protection rules in GitLab Flow?

Branch rules can restrict who pushes or merges and require successful checks. They govern Git changes. Deployment controls are separate: GitLab protected environments can limit who may deploy and require deployment approvals. Neither control proves that the exact artifact tested in staging is the one sent to production, so record its digest and promote the same artifact through both jobs. See GitLab protected environments for the environment-side controls.

12. How do you handle long-running staging validation without blocking development?

Long staging validation can delay a release. Teams can parallelize independent tests and run quick smoke checks first. If development continues while validation is underway, track the exact artifact digest in staging; do not assume that a later merge to main is the candidate that was approved.

13. When should you choose release-based GitLab Flow over environment-based?

Environment branches can suit a team that chooses to track environment state in Git, provided deployments also use artifact identity, environment protection, and approval rules. Release branches can suit products with scheduled versions or multiple supported customer releases. If you deploy a single version from short-lived branches, GitHub Flow may be simpler. See also release branching and hotfixes.

14. How does GitLab Flow handle rollback scenarios when a production deployment fails?

Rollback in environment-based GitLab Flow is straightforward: you revert the production merge by creating a new merge from the previous known-good commit to production, or by using git revert on the merge commit. In GitLab, you can reopen the previous merge request or create a new rollback MR. The pipeline's smoke tests on production should catch the failure within minutes, triggering an alert. Automating rollback scripts that run on pipeline failure reduces mean time to recovery.

15. What metrics should you track to measure GitLab Flow effectiveness?

Key metrics include: lead time (time from commit to production), deployment frequency (deploys per day/week per environment), change failure rate (percentage of deploys causing production failures), mean time to recovery (how fast rollbacks happen), and staging-to-production promotion time (how long code sits in staging before approval). These metrics align with DORA benchmarks and reveal bottlenecks in the pipeline.

16. What is the difference between `when: manual` and automatic deployment in GitLab CI/CD?

when: manual makes a job wait for a user to start it, subject to the project’s configuration and permissions. It is one way to add a production gate, but approval rules and protected-environment access can also control deployment. Whether staging deploys automatically depends on the pipeline’s rules; a merge to main does not trigger every project configuration.

17. How does GitLab Flow handle version drift between staging and production environments?

Track the deployed source SHA and artifact digest for each environment. Branch ancestry can help explain code history, but it does not show which build is running. If staging and production differ, compare their recorded digests, then promote the approved artifact or create a corrected release according to the team’s rollback policy.

18. What is the role of GitLab's pipeline DAG in enforcing GitLab Flow's environment hierarchy?

A pipeline DAG can order tests, builds, and deployment jobs, but it does not by itself enforce human approval or protect an environment. Configure protected environments and approval rules separately, and pass the same artifact digest from staging to production rather than rebuilding from a later branch head.

19. How do you handle feature flags within GitLab Flow's environment-based branching model?

Feature flags control who can see a feature after its code has been deployed. They do not make a branch an approval gate or prove that an artifact passed validation. A team can build once, promote the same artifact through protected environments, enable the flag for a small group, and expand access after monitoring results. Remove the flag when it is no longer needed.

20. How does GitLab Flow's merge-back policy prevent production branch staleness?

Teams that maintain separate environment branches need a documented path for production fixes to reach active development and other supported versions. CI can check the chosen merge-back rule, but ancestry alone does not prove which artifact was deployed or that a hotfix was included in every maintained version. Track the source SHA and digest in deployment records.

Further Reading

Conclusion

GitLab Flow describes a family of workflows rather than one required branch diagram. Environment branches can fit teams that deliberately track environment state in Git; release branches can fit versioned software and multiple support lines. Deployment safety comes from the surrounding controls: test and record the artifact, promote the same digest, protect environments, require approvals when needed, and define rollback. For a single continuously delivered version, GitHub Flow may require less branch overhead.

Category

Related Posts

Git Flow: The Original Branching Strategy Explained

Master the Git Flow branching model with master, develop, feature, release, and hotfix branches. Learn when to use it, common pitfalls, and production best practices.

#git #version-control #branching-strategy

Automated Changelog Generation

Generate release notes from structured commits, choose a release policy, and add changelog generation to CI without treating every merge as a release.

#git #version-control #changelog

Choosing a Git Team Workflow

Compare trunk-based development, short-lived feature branches, and release branches against your release, review, rollback, and compliance needs.

#git #version-control #branching-strategy