Recently, I have been working on developing specifications to get the project up and running efficiently. With plans to introduce Git version control, Git-flow is the first choice. I haven’t touched much before, so take some time to feel it out.

Why git-flow

Agreeing on a common workflow is critical when using a version control system in team development. Git does do a lot of things on all fronts, but if you don’t have a specific and effective workflow in place on your team, chaos is inevitable.

The basics: You can define a workflow that fits your project perfectly, or use one that someone else has defined.

Install git-flow

We use Homebrew to install Git-flow:

brew install git-flowCopy the code

After that, use git-flow to initialize the project:

git flow initCopy the code

In this case, there will be some configuration operations, the default operation:

Initialized empty Git repository in /Users/jartto/Documents/git-flow-demo/.git/ No branches exist yet. Base branches must be created now. Branch name for production releases: [master] Branch name for "next release" development: [develop] *** Please tell me who you are. Run git config --global user.email "[email protected]" git config --global user.name "Your Name" to set your account's default identity. Omit --global to set the identity only in this repository.  fatal: unable to auto-detect email address (got 'jartto@bogon.(none)') fatal: Not a valid object name: 'master'. error: pathspec 'develop' did not match any file(s) known to git. How to name your supporting branch prefixes? Feature branches? [feature/] Release branches? [release/] Hotfix branches? [hotfix/] Support branches? [support/] Version tag prefix? []Copy the code

It’s important to note that git-flow simply encapsulates git commands.

So when we initialized, no other changes were made to the repository, just a few branches were created. Of course, if you don’t want to continue using Git-flow, you can simply disable git-flow without modifying or deleting any files.

To verify this, let’s take a look at the current branch and execute:

git branchCopy the code

Output:

* develop
  masterCopy the code

Quite simply, the Develop branch is our daily development branch, with lots of changes. The master is for the online branch, which will be discussed below.

As a bonus, we can view the use cases through the help command.

git flow helpCopy the code

The output is as follows:

usage: git flow <subcommand>
Available subcommands are:
   init      Initialize a new git repo with support for the branching model.
   feature   Manage your feature branches.
   release   Manage your release branches.
   hotfix    Manage your hotfix branches.
   support   Manage your support branches.
   version   Shows version information.
Try 'git flow <subcommand> help' for details.Copy the code

What if I want to see feature-related commands?

git flow feature helpCopy the code

The usage specification will list:

usage: git flow feature [list] [-v]
       git flow feature start [-F] <name> [<base>]
       git flow feature finish [-rFk] <name|nameprefix>
       git flow feature publish <name>
       git flow feature track <name>
       git flow feature diff [<name|nameprefix>]
       git flow feature rebase [-i] [<name|nameprefix>]
       git flow feature checkout [<name|nameprefix>]
       git flow feature pull <remote> [<name>]Copy the code

Third, branch mode

Git-flow mode presuppresses two main branches in the repository: 1. Master can only be used to contain production code. We cannot work on this master branch directly, but on other designated, independent feature branches.

It is also a common rule of many workflows not to commit changes directly to the Master branch.

When you start a new feature branch, it will be the basis for development. In addition, this branch brings together all functionality that has been completed and is waiting to be integrated into the Master branch.

These two branches are referred to as long-term branches, and they will survive throughout the life of the project.

Other branches, such as the feature branch, the release branch, are only temporary. They are created on demand and removed when they have completed their tasks.

Four, clear branch function

1. Master branch is the most stable functional complete ready to release code, that is, the code development is completed, after testing, no obvious bug, can be merged into the master. Be careful never to develop and commit code directly on the Master branch to ensure that code on master is always available;

The develop branch is used as the main branch of normal development and always exists. It contains all the code to be released to the next release and is used to merge other branches, such as the feature branch. If you want to change the code, create a new feature branch and merge it into the Develop branch. All feature and Release branches are pulled from the Develop branch.

This branch is mainly used to develop new features. Once the development is complete and it passes the test, we will merge back into the Develop branch and enter the next release.

4. Release branch A specialized branch for release preparation. When development has progressed to a point where it is ready to be released, or when a certain release date is reached, create a Release branch and specify a version number (this can be added when Finish is available). Developers can centrally test and bug fix code on the Release branch. After all the tests are done and there are no problems, merge the code on the Release branch into the Master branch and the Develop branch.

5. The Hotfix branch is used to fix bugs in online code. Pull it up from the master branch. So once we’re done with the hotfix, we’ll tag it and we’ll merge back into the Master and Develop branches.

Note:

  • All development branches fromdevelopBranch.
  • allhotfixBranch frommasterPull.
  • All inmasterAll submissions are requiredtagTo facilitate rollback.
  • As long as there is a merge tomasterThe operation of a branch requires a sumdevelopBranch merging ensures synchronization.
  • masterdevelopBranches are the main branches. There can only be one main branch of each type, and multiple derived branches of each type can exist at the same time.

V. About Feature branch

In Git-flow, the use of Feature branches makes it easier to develop multiple branches at the same time. We received a request for Test1 and started it with feature start:

git flow feature start test1

When we start a new feature development:

Switched to a new branch 'feature/test1'
Summary of actions:
- A new branch 'feature/test1' was created, based on 'develop'
- You are now on branch 'feature/test1'
Now, start committing on your feature. When done, use:
     git flow feature finish test1Copy the code

We automatically cut to the feature/test1 branch, just in time to start our development, create a file:

vi main.jsCopy the code

Written to the console. The log (‘ hello jartto ‘); Next commit our changes:

git add .
git commit -m 'feat: add console.log'Copy the code

Ok, now we are done, end feature development:

git flow feature finish test1Copy the code

The console output:

Switched to branch 'develop' Updating d975789.. 27e920c Fast-forward main.js | 1 + 1 file changed, 1 insertion(+) create mode 100644 main.js Deleted branch feature/test1 (was 27e920c). Summary of actions: - The feature branch 'feature/test1' was merged into 'develop' - Feature branch 'feature/test1' has been removed - You are now on branch 'develop'Copy the code

1. Merge the feature/test1 branch into the Develop branch; 2. Delete feature/test1; Switch to the Develop branch.

Note that git-flow uses the following command:

Git merge - no - ff feature/test1Copy the code

feature

Git merge — ff –no-ff — Git merge –no-ff — Git merge

Next, let’s look at the change record:

git log --onelineCopy the code

Output:

27e920c (HEAD -> develop) feat: add console.log
d975789 (master) Initial commitCopy the code

Release branch – Version release

When we finish development and need to release a new version, we can use:

Git flow release start 0.1.0Copy the code

The console will have some hints:

Switched to a new branch 'release/0.1.0' Summary of actions: - A new branch 'release/0.1.0' was created, Based on 'develop' - You are now on branch 'release/0.1.0' follow-up actions: - Bump the version number now! Start research case of last-minute fixes in preparing your releasewhen done, run: Git flow release finish '0.1.0'Copy the code

Create a release/0.1.0 branch based on the Develop branch. 2. Switch to the Release /0.1.0 branch

Bump the version number now! 2. What does last-minute fixes mean?

So what are we going to do next? Take your time and follow the instructions step by step.

After we modify the code, add and commit, execute:

Git flow release Finish 0.1.0Copy the code

Exceptions may occur during this process:

fatal: no tag message?
Tagging failed. Please run finish again to retry.Copy the code

Output:

Switched to branch 'develop'
Merge made by the 'recursive' strategy.
test.js | 0
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 test.js
Deleted branch release/0.1.0 (was 0426707).
Summary of actions:
- Latest objects have been fetched from 'origin'
- Release branch has been merged into 'master'
- The release was tagged '0.1.0'
- Release branch has been back-merged into 'develop'
- Release branch 'release/0.1.0' has been deletedCopy the code

1. First, git-flow pulls the remote repository to make sure it is currently the latest version. 2. The release content is then merged into the Master and Develop branches so that not only is the production code the latest version, but the new functionality branch is based on the latest code. 3. For easy identification and historical reference, the release teaser is marked with the release Tag. 4. Clean up, the version branch is deleted and goes back to Develop.

Hotfix code

What if there is a problem with the online code and you need to fix it urgently?

Git flow hotfix

git flow hotfix start jarttoCopy the code

Take a look at what is executed:

Switched to a new branch 'hotfix/jartto'
Summary of actions:
- A new branch 'hotfix/jartto' was created, based on 'master'
- You are now on branch 'hotfix/jartto'
Follow-up actions:
- Bump the version number now!
- Start committing your hot fixes
- When done, run:
     git flow hotfix finish 'jartto'Copy the code

Next we create a new directory:

mkdir assetsCopy the code

Add an image, modify it, submit and end hotfix:

git add .
git commit -m 'fix: assets img'
git flow hotfix finish 'jartto'Copy the code

Git-flow:

Switched to branch 'master'
Merge made by the 'recursive' strategy.
assets/git-flow.png | Bin 0 -> 25839 bytes
1 files changed, 0 insertions(+), 0 deletions(-)
create mode 100644 assets/git-flow.png
Switched to branch 'develop'
Merge made by the 'recursive' strategy.
assets/git-flow.png | Bin 0 -> 25839 bytes
1 files changed, 0 insertions(+), 0 deletions(-)
create mode 100644 assets/git-flow.png
Deleted branch hotfix/jartto (was a734849).
Summary of actions:
- Latest objects have been fetched from 'origin'
- Hotfix branch has been merged into 'master'
- The hotfix was tagged 'jartto'
- Hotfix branch has been back-merged into 'develop'
- Hotfix branch 'hotfix/jartto' has been deletedCopy the code

Check the current Tag status:

git tagCopy the code

These are the two tags we added above:

0.1.0 from jarttoCopy the code

To summarize: 1. The completed changes are merged into the Master, as well as into the Develop branch, to ensure that this error does not reappear in the next release. 2. The Hotfix program will be marked for easy reference. 3. The Hotfix branch will be removed and switched to the Develop branch.

Git-flow flow diagram

Congratulations, you’ve done it heregit-flowBasic flow. To understand the workflow more generally, let’s look at the following flow chart:

To be clear, is it the same process as we did above?

Nine, reference

Learn Version Control with Git Using Git-flow to your Git shoot workflow