Mastering git, Part 14, git-flow and GitHub flow

Git-Flow and GitHub Flow

Both Git-Flow and GitHub Flow are workflow models that guide how software development teams use Git for version control. Each has its own structure and rules for managing branches, which can influence how teams collaborate and deploy software.

Git-Flow

Git-Flow is a branching model for Git, designed by Vincent Driessen. It provides a robust framework for managing larger projects. Git-Flow is best suited for projects that have a scheduled release cycle. The main branches in Git-Flow are:

  1. Master Branch: This branch contains production-ready code.
  2. Develop Branch: This branch is for development and contains the latest delivered development changes.
  3. Feature Branches: These are branched off from the develop branch for new features.
  4. Release Branches: Branched from the develop branch to prepare for a production release.
  5. Hotfix Branches: Branched from the master branch to quickly patch production releases.

GitHub Flow

GitHub Flow is a simpler alternative developed and used by GitHub. It’s more lightweight and is well-suited for Continuous Integration/Continuous Deployment (CI/CD) environments. It usually involves the following steps:

  1. Anything in the main branch is deployable.
  2. Create descriptive branches off of main for new features or fixes.
  3. Commit changes to these branches.
  4. Open a pull request to merge these changes into the main branch.
  5. After someone else reviews and approves the pull request, merge it into main.
  6. Once merged, the changes are immediately deployed to production.

Example Scenario

Imagine you’re working on a web application. Here’s how both workflows would handle adding a new feature, such as a user login system:

Git-Flow

  1. Create a Feature Branch: From the develop branch, create a new branch called feature/user-login.
  2. Develop the Feature: Work on the user login system in this branch.
  3. Merge with Develop: Once complete, merge feature/user-login back into the develop branch.
  4. Create a Release Branch: When ready to release, create a release branch from develop.
  5. Beta Testing and Fixes: Perform final testing and bug fixes in the release branch.
  6. Merge into Master and Develop: Merge the release branch into master and develop, then tag the release in master.

GitHub Flow

  1. Create a Branch: From the main branch, create a new branch called user-login.
  2. Develop the Feature: Implement and commit the user login system changes in this branch.
  3. Open a Pull Request: Once the feature is ready, open a pull request to merge user-login into main.
  4. Review and Deploy: After code review and approval, merge the branch into main.
  5. Deploy to Production: The changes in main are deployed immediately.

Here is a simple, practical example for both Git-Flow and GitHub Flow, demonstrating the key commands used in each workflow.

Git-Flow Example

1. Initializing Git-Flow in a Repository

git flow init is a command used in conjunction with the Git-Flow extension for Git, which is not a standard part of Git itself. This command initializes a Git repository with the structure and branches needed for the Git-Flow workflow. Here’s what it does:

  1. Initializes the Repository (if not already a Git repository): If the repository hasn’t been initialized with Git (git init), git flow init will do this first.

  2. Sets Up Branching Structure: Git-Flow uses a specific set of branches (like develop, feature/, release/, hotfix/, and master) to manage the development workflow. The git flow init command sets up this structure.

  3. Configures Naming Conventions: During initialization, you can configure the naming conventions for various types of branches (features, releases, hotfixes). By default, it uses standard names like feature/, release/, and hotfix/ as prefixes.

  4. Version Tag Prefix: You can also set a prefix for tagging releases (e.g., v1.0.0).

To use git flow init, you first need to install the Git-Flow extension. This is typically done through a package manager in your operating system. For example, on macOS, you can use Homebrew:

on Ubuntu:

After installing Git-Flow, you can run git flow init inside your Git repository to start using the Git-Flow workflow. The command prompts you to define branch names and other configurations, but you can also accept the default settings which are suitable for most projects.

2. Starting a New Feature

This creates a new feature branch from develop.

3. Completing the Feature

This merges the feature into develop.

4. Starting a Release

This creates a release branch from develop.

5. Completing the Release

This merges the release into master and develop, and tags the release.

6. Hotfixes (If Needed)

This creates a hotfix branch from master, then merges the changes back into both master and develop.

GitHub Flow Example

1. Creating a New Feature Branch

This creates and switches to a new branch from main.

2. Making Changes and Committing

3. Pushing Changes and Creating a Pull Request

After pushing, you’ll create a pull request on GitHub’s website to merge into main.

4. Review, Merge and Deploy

After the pull request is reviewed and approved, you merge it into main.

5. Pulling the Changes to Local main Branch

This updates your local main branch with the merged changes.

Manually Implementing Git-Flow Without the Git-Flow Command

you do not have to use the git flow command or the Git-Flow extension to follow the Git-Flow workflow. You can manually replicate the Git-Flow process using standard Git commands. The git flow command simply provides convenience by automating some of the branching and merging steps according to the Git-Flow model.

1. Initialize Your Repository

If you haven’t already, initialize your Git repository:

2. Create a Develop Branch

After initializing, create a develop branch from master:

3. Feature Branches

When starting work on a new feature:

Make changes and commit them to this branch. Once the feature is complete, merge it back into develop:

4. Release Branches

When ready to prepare a release:

After final tweaks and commits:

5. Hotfix Branches

If there’s a need for a hotfix on master:

After the fix:

Using Git-Flow on GitHub

  1. Push Your Branches: After creating branches locally (like develop, feature branches, etc.), push them to GitHub.

  2. Pull Requests for Features: For each feature branch, open a pull request to merge it into develop. This allows for code review and collaboration.

  3. Releases and Hotfixes: When releasing or creating hotfixes, follow the same branching and merging process. You can also create pull requests for these branches if you want to include code review.

  4. Tags for Releases: When you merge a release into master, tag the commit with the release version. This helps in tracking releases directly on GitHub.

  5. GitHub Actions for CI/CD: You can integrate GitHub Actions to automate your CI/CD pipeline, running tests, and deploying your code when changes are pushed to specific branches like master or develop.

Following Git-Flow manually requires discipline and understanding of the flow, but it’s entirely feasible and gives you full control over your Git operations. Whether you choose the automated tool or manual process, the key is consistency in following the workflow.

In summary, Git-Flow is more structured and is well-suited for complex projects with scheduled releases, while GitHub Flow is simpler, encourages continuous deployment, and is more adaptable for smaller or continuous delivery projects.

0 0 votes
Article Rating
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x