Skip to content

Instantly share code, notes, and snippets.

@tarasowski
Last active February 6, 2024 06:55
Show Gist options
  • Save tarasowski/3361fa3c8b0bece7d166f8d3d2714bb3 to your computer and use it in GitHub Desktop.
Save tarasowski/3361fa3c8b0bece7d166f8d3d2714bb3 to your computer and use it in GitHub Desktop.
Trunk-based development feature flag

In a trunk-based development approach with short-lived feature branches, the process would be similar, but with a slight difference in the workflow. Here's how it would look:

  1. Create a Short-Lived Feature Branch: You start by creating a new branch for your feature from the main branch (trunk).

    git checkout -b dark-mode-feature
  2. Introduce the Feature Flag: In your new branch, you introduce a new feature flag in your code:

    const enableDarkMode = false; // This is your feature flag

    And then in the part of your code that determines the theme of your application, you would check this feature flag:

    if (enableDarkMode) {
      // Code to enable dark mode
    } else {
      // Code to enable light mode (the default)
    }
  3. Commit and Push Your Changes: As you make progress on your "Dark Mode" feature, you commit and push your changes to the dark-mode-feature branch.

    git add .
    git commit -m "Progress on dark mode feature"
    git push origin dark-mode-feature
  4. Merge into Main: Once your "Dark Mode" feature is finished and tested, you merge your feature branch into the main branch.

    git checkout main
    git pull origin main
    git merge dark-mode-feature
    git push origin main
  5. Enable the Feature: Now, you can "turn on" the feature by setting enableDarkMode to true in the main branch and deploying your application.

This approach still allows you to integrate your changes into the main codebase frequently and in small batches, but with the added benefit of isolating your changes in a separate branch until the feature is complete. This can help keep the main branch stable and prevent unfinished features from affecting the live application.

Note: Feature flags allow you to merge code into the main branch without affecting the existing functionality of your application.

When a new feature is under development and is not ready to be used in production, you can hide it behind a feature flag. This means the code for the feature is present in the codebase, but it's not executed unless the flag is enabled.

This allows you to regularly merge your changes into the main branch and avoid long-lived feature branches, which can often lead to difficult merge conflicts. It also allows you to test the new feature in the production environment without exposing it to end users.

Once the feature is fully developed and tested, you can "turn on" the feature by enabling the flag. If there are any issues with the feature, you can easily "turn off" the feature by disabling the flag, without needing to deploy a new version of the application.

So, feature flags provide a way to separate feature rollout from code deployment, giving you more control and flexibility over your application's features.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment