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:
- Master Branch: This branch contains production-ready code.
- Develop Branch: This branch is for development and contains the latest delivered development changes.
- Feature Branches: These are branched off from the develop branch for new features.
- Release Branches: Branched from the develop branch to prepare for a production release.
- 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:
- Anything in the
main
branch is deployable. - Create descriptive branches off of
main
for new features or fixes. - Commit changes to these branches.
- Open a pull request to merge these changes into the
main
branch. - After someone else reviews and approves the pull request, merge it into
main
. - 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
- Create a Feature Branch: From the
develop
branch, create a new branch calledfeature/user-login
. - Develop the Feature: Work on the user login system in this branch.
- Merge with Develop: Once complete, merge
feature/user-login
back into thedevelop
branch. - Create a Release Branch: When ready to release, create a
release
branch fromdevelop
. - Beta Testing and Fixes: Perform final testing and bug fixes in the release branch.
- Merge into Master and Develop: Merge the release branch into
master
anddevelop
, then tag the release inmaster
.
GitHub Flow
- Create a Branch: From the
main
branch, create a new branch calleduser-login
. - Develop the Feature: Implement and commit the user login system changes in this branch.
- Open a Pull Request: Once the feature is ready, open a pull request to merge
user-login
intomain
. - Review and Deploy: After code review and approval, merge the branch into
main
. - 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
1 |
git flow init |
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:
-
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. -
Sets Up Branching Structure: Git-Flow uses a specific set of branches (like
develop
,feature/
,release/
,hotfix/
, andmaster
) to manage the development workflow. Thegit flow init
command sets up this structure. -
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/
, andhotfix/
as prefixes. -
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:
1 |
sudo apt-get install git-flow |
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
1 |
git flow feature start user-authentication |
This creates a new feature branch from develop
.
3. Completing the Feature
1 2 3 4 5 6 |
# Make changes and commit them git add . git commit -m "Add user authentication feature" # Finish the feature git flow feature finish user-authentication |
This merges the feature into develop
.
4. Starting a Release
1 |
git flow release start 1.0.0 |
This creates a release branch from develop
.
5. Completing the Release
1 2 3 4 5 6 |
# Make final changes (if any), then commit git add . git commit -m "Final touches for release 1.0.0" # Finish the release git flow release finish 1.0.0 |
This merges the release into master
and develop
, and tags the release.
6. Hotfixes (If Needed)
1 2 3 4 5 |
git flow hotfix start emergency-fix # Make the hotfix changes git add . git commit -m "Emergency fix for production issue" git flow hotfix finish emergency-fix |
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
1 2 3 |
git checkout main git pull git checkout -b add-login-feature |
This creates and switches to a new branch from main
.
2. Making Changes and Committing
1 2 3 |
# Make changes to the code git add . git commit -m "Implement login feature" |
3. Pushing Changes and Creating a Pull Request
1 |
git push origin add-login-feature |
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
1 2 |
git checkout main git pull |
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:
1 |
git init |
2. Create a Develop Branch
After initializing, create a develop
branch from master
:
1 2 |
git checkout -b develop git push -u origin develop |
3. Feature Branches
When starting work on a new feature:
1 2 |
git checkout develop git checkout -b feature/your-feature-name |
Make changes and commit them to this branch. Once the feature is complete, merge it back into develop
:
1 2 3 4 |
git checkout develop git merge feature/your-feature-name git branch -d feature/your-feature-name git push origin develop |
4. Release Branches
When ready to prepare a release:
1 2 |
git checkout develop git checkout -b release/1.0.0 |
After final tweaks and commits:
1 2 3 4 5 6 |
git checkout master git merge release/1.0.0 git tag -a 1.0.0 git checkout develop git merge release/1.0.0 git branch -d release/1.0.0 |
5. Hotfix Branches
If there’s a need for a hotfix on master
:
1 2 |
git checkout master git checkout -b hotfix/urgent-fix |
After the fix:
1 2 3 4 5 6 |
git checkout master git merge hotfix/urgent-fix git tag -a 1.0.1 git checkout develop git merge hotfix/urgent-fix git branch -d hotfix/urgent-fix |
Using Git-Flow on GitHub
-
Push Your Branches: After creating branches locally (like
develop
, feature branches, etc.), push them to GitHub. -
Pull Requests for Features: For each feature branch, open a pull request to merge it into
develop
. This allows for code review and collaboration. -
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.
-
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. -
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
ordevelop
.
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.