Understanding of Git Flow
Understanding of three kinds of git flow
Why should we use the git branching strategy?
Let us imagine a scenario. What happens when a team develops features or fixes bugs on the same project without a standardized branching strategy? Two programmers may both develop new features on the main branch, but because they modified the same file, a more serious conflict occurs. To resolve these conflicts needs extra time which will slow down development progress. Even if everyone writes their code on their branch, if there is no standardized branching strategy, it will be impossible to know the progress of each branch. There's no clear way to know which features are complete and which are still in development, which can lead to confusing releases.
Therefore, a branching strategy becomes very important. What is a branching strategy? Developers follow a development guideline and a strategy that software development teams adopt when writing, merging, and deploying code using a version control system.
There are three common branching strategies in Git, which are Git flow, GitLab flow, and GitHub flow.
Git Flow
First, the detailed process of Git Flow is as follows:
A. Main branch: used to store published and stable code.
B. Development branch: stores the latest developed code, and is the routine daily work branch for developers.
C. Feature branch: Created from the development branch and used to develop new features. Developers work on feature branches, and when the function is completed, it will be merged back into the development branch through a pull request.
D. Release branch: used for new version release, final testing, and bug fixing. When the code on the release branch is ready, it will merge into the main branch. Meanwhile, we can tag the released version, such as version 1.0. When next time we want to develop, we will pull the latest code from main to develop.
E. Hotfix branch: used to fix urgent problems and after the fix is made on the hotfix branch, it will be merged back into the main and develop branches. This ensures synchronization in both production and development environments.
Git Flow branching strategy has the following advantages, disadvantages, and using scenarios.
Advantages:
The branch structure is clear and easy to manage and maintain.
The develop branch is used for daily development, the main branch is used to release stable versions, the release branch is used for preparations before release, and the hotfix branch is used to quickly fix emergency problems.
Supports parallel work and independent development of respective functions.
Disadvantages:
Complex structure: too complex for small teams or simple projects.
Long release cycle: The release process is slow, not good for iteration, and not flexible enough for small projects requiring frequent releases.
Using scenarios:
Complex large-scale projects that require a clear branching structure to manage different features and versions.
Projects that don't require frequent releases because Git Flow might be too cumbersome and inflexible.
GitHub Flow
The detailed process of GitHub Flow is as follows:
A. Main branch: Used to store production code and always maintain a deployable state.
B. Feature branch: Used to develop new features.
Developers create a new branch from the main and can develop on this new branch. Each time a development task is completed, the modifications are recorded in a branch. After development is completed, submit a pull request to the main branch, and other team members can see the modifications and review them. After the code review passes, the pull request is approved and ready to be merged into the master branch. Then we could deploy the code to a test or production environment. Finally, merge the code of the feature branch into the main branch.
GitHub Flow branching strategy has the following advantages, disadvantages, and using scenarios.
Advantages:
Simple structure and easy to implement.
Feature development and release can happen quickly.
Each time function development is completed, it can be merged into the main branch immediately.
Disadvantages:
The branch structure is simple and not suitable for managing complex, large-scale projects.
There is no hotfix branch, emergency fixes need to be made on the feature branch or the main branch, which may affect other work.
Using scenarios:
- Small projects or teams that need to iterate and release quickly.
GitLab Flow
Gitlab Flow is similar to Github Flow. It also has the main branch and the feature branch, and developers develop in the feature branch. It is in the Pre-Production branch for testing and bug fixes. Finally, create the production branch from the pre-production branch and then publish it to the production environment.
Advantages:
The branch structure is relatively simple and easy to understand and manage.
Can be tested in a pre-production environment to quickly find and fix problems.
Suitable for projects of all sizes and with flexible strategies.
Disadvantages:
- Branches and merge requests need to be strictly managed to ensure good code quality and stability.
Using scenarios:
- Suitable for small and medium-sized teams.
Some Git Instructions
git init: perform initialization.
git status: View the status of files in the management directory.
git add "filename": manage specified files (red zone to green zone).
git config --global user.email "you@example.com": personal information configuration.
git commit -m "description information": generated version.
git log: View version records.
Roll back to the previous version:
git log: View version number.
git reset -hard: add version number.
Roll back to a later version:
git reflog: View version number.
git reset -hard: add version number.
Branches and urgent bug fixes:
git branch: view branches.
git branch "branch name" : create branch.
git switch "branch name": switch branch.
git merge branch: merge branch.
git branch -d "branch name": delete branch
Code hosting based on github:
git remote add origin "remote warehouse address": give the remote warehouse a name.
git push -u origin branch: push code to the remote.
git clone "remote warehouse address": clone the remote warehouse code.
git switch: switch branches.
How to synchronize company code at home:
git switch dev: switch to the development branch.
git merge main: merge main into dev.
git pull origin dev: pull code (equivalent to "git fetch orgin dev" + "git merge origin dev" ).
git push origin dev: push code to remote
Reference
[1] What Are the Best Git Branching Strategies: https://www.abtasty.com/blog/git-branching-strategies/
[2] Getting started with branching workflows, Git Flow and GitHub Flow:
https://www.youtube.com/watch?v=gW6dFpTMk8s&ab_channel=NickChapsas
[3] Git-Flow, GitHub-Flow, Gitlab-Flow and Trunk Based Development explained: https://steven-giesel.com/blogPost/ff50f268-c0bf-44d8-a5b8-41554ab50ba8
Subscribe to my newsletter
Read articles from Ziqing Ouyang directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by