Skip to content

FAQ: Git Flow Support for Multiple mainlines

Ulrond edited this page Jun 20, 2024 · 1 revision

Git Flow Supporting for Multiple mainlines

Git Flow enhances the Git workflow by standardizing branching conventions and providing helpful commands for managing features, releases, and hotfixes. This manual guides you through understanding and automating Git Flow configuration, where you can use this to re-configure your development mainline and have multiple configurations.

Understanding Git Flow Variables

Git Flow operates by storing configuration variables within each repository's Git configuration. When you initialize Git Flow using git flow init -d (with defaults), it sets up these variables in the .git/config file.

Viewing Configuration:

You can inspect the current configuration in two ways:

  1. Using Git:

    git config --list | grep gitflow 

    This will output lines like:

    gitflow.branch.master=main
    gitflow.branch.develop=develop
    gitflow.prefix.feature=feature/
    ...
    
  2. Using Git Flow:

    git flow config

    This provides a more user-friendly display:

    Branch name for production releases: main
    Branch name for "next release" development: develop
    Feature branch prefix: feature/
    ...
    

Automating Configuration with Shared File

1. Create the Shared Configuration File (<project_root>/.gitflowconfig):

This file defines your Git Flow settings. Here's an example supporting dual main/develop branches:

<project_root>/.gitflowconfig

master=main  
develop=develop 
feature=feature/
bugfix=bugfix/  
release=release/
hotfix=hotfix/
support=support/ 
versiontag=

<project_root>/.gitflowconfig2

master=main2
develop=develop2
feature=feature/
bugfix=bugfix/  
release=release/
hotfix=hotfix/
support=support/ 
versiontag=

2. Write the Automation Script (init_gitflow.sh):

#!/bin/bash

# Optionally specify a custom Git Flow configuration file path
custom_config_file=""
if [[ $# -gt 0 ]]; then
  custom_config_file=$1
fi

# Check if Git Flow is already initialized 
if [[ $(git config --get gitflow.branch.master) ]]; then
  echo "Git Flow is already initialized."
  exit 0  # Exit successfully if already initialized
fi

# Load configuration from .gitflowconfig or the specified file
if [[ -n "$custom_config_file" ]]; then
  if [[ -f "$custom_config_file" ]]; then
    source "$custom_config_file"
    echo "Using custom Git Flow configuration from $custom_config_file"
  else
    echo "Error: Custom configuration file not found: $custom_config_file"
    exit 1
  fi
else
  if [[ -f .gitflowconfig ]]; then
    source .gitflowconfig
    echo "Using default Git Flow configuration from .gitflowconfig"
  else
    echo "Error: Neither default nor custom Git Flow configuration file found."
    exit 1
  fi
fi

# Initialize Git Flow using defaults (-d)
git flow init -d

# Set the configuration parameters from .gitflowconfig or the specified file
git config gitflow.branch.master $master
git config gitflow.branch.develop $develop
git config gitflow.prefix.feature $feature
git config gitflow.prefix.bugfix $bugfix         # Set bugfix prefix
git config gitflow.prefix.release $release       # Set release prefix
git config gitflow.prefix.hotfix $hotfix         # Set hotfix prefix
git config gitflow.prefix.support $support       # Set support prefix
git config gitflow.prefix.versiontag $versiontag # Set versiontag prefix

echo "Git Flow initialized successfully!"

3. Make the Script Executable:

chmod +x init_gitflow.sh

4. Run the Script: (using default configuration)

./init_gitflow.sh

or specify a global / alternative configuration

./init_gitflow.sh ~/.gitflowconfig2

Leveraging Git Flow in Your Workflow

The variables develop and main are setup based on configuration, allowing multiple mainlines to be supported.

Using Feature Branches

# Start a new feature branch from variable 'develop'
git checkout $(git config --get gitflow.branch.develop)
git flow feature start <feature_name> 
# ... (develop your feature)
# Finish the feature branch, merging it into 'develop'
git flow feature finish <feature_name> 

Release Branches:

# Start a new release branch from variable 'develop'
git checkout $(git config --get gitflow.branch.develop)
git flow release start <version_number>
# ... (perform final testing)
autochange-log -v <version_number> # generates release notes
# Finish the release branch, merging into both variable 'develop' AND variable 'main', and pushing changes
git flow release finish <version_number> 
git push 
git push --tags
git checkout $(git config --get gitflow.branch.main) # push to variable `main`
git push
git checkout $(git config --get gitflow.branch.develop) # push to variable `develop`

Scaling Git Flow Initialization with og (Optional)

If you have the sc (Source Control) scripts installed, you can leverage the og (Operate on Git) command to effortlessly initialize Git Flow across multiple repositories within your project's directory structure.

Here's how it works:

  1. Prerequisites:

    • sc Scripts: Ensure you have the sc scripts installed. You can find them on GitHub or package managers for your system.
    • init_gitflow.sh and .gitflowconfig: Make sure these files are located in your home directory (~).
  2. Run the og Command:

    og cmd "~/init_gitflow.sh ~/.gitflowconfig"

    This command does the following:

    • Starting Point: Begins at your current directory.
    • Recursive Search: Looks for all Git repositories within the current directory and its subdirectories.
    • Execution: In each found repository, it runs the ~/init_gitflow.sh script. The script will use the shared configuration file ~/.gitflowconfig to set up Git Flow consistently.

Example Usage:

Let's say your project has the following structure:

my_project/
├── frontend/
│   └── .git
├── backend/
│   └── .git
└── docs/

By running og cmd "~/init_gitflow.sh ~/.gitflowconfig" from within my_project/, you'll initialize Git Flow in both the frontend and backend repositories, applying the settings from your shared configuration file.

Clone this wiki locally