Why

Skilled use of Git branch operation is one of the essential skills of Git, especially for multi-person cooperative projects, branch management and operation is particularly important.

Why do we need branches? Assume that feature 1 (Feature /1) has been developed on the basis of the existing project and has been half completed; But if a Bug is found online (Hotfix /1) or the product manager temporarily needs to add a small feature/2 (Feature /2), how can you develop code without Func_1? Git branch provides us with a solution as follows:

Pull branch feature/1 from online code to start the development of function 1;

Pull branch hotfix/1 from online code, start Fix;

Pull the branch feature/2 from the online code to start the development of feature 2…

As you can see, branches are independent and can be assigned to multiple people, and Git has no limit on the number of branches.

Git encourages the practice of using branches multiple times to accomplish a task, merging them and then deleting them.

If you still don’t understand the meaning of branch, please read the following excerpt from Liao Xuefeng -Git tutorial – branch management

A branch is a parallel universe in a science fiction movie, where you are trying to learn Git while you are trying to learn SVN in another parallel universe.

If the two universes didn’t interfere with each other, it wouldn’t matter to you right now. At some point, though, the two parallel universes merged, and as a result, you learned both Git and SVN.

What does branching do in practice? Let’s say you want to develop a new feature, but it takes two weeks to complete. In the first week, you write 50% of the code. If you submit it immediately, the incomplete code base will cause others to be unable to work because the code is not finished. If you wait until all the code is written and then commit again, you run the risk of losing progress on a daily basis.

Now that we have branches, we don’t have to be afraid. You create a branch of your own, which no one else can see, and continue to work on the original branch, while you work on your own branch, commit as you like, until the development is finished, and then merge into the original branch once, so that it is safe and does not affect the work of others.

Other version control systems such as SVN have branch management, but once you use them you will find that they are intolerably slow at creating and switching branches. As a result, the branch function becomes obsolete and nobody uses it.

But Git branches are different. You can create, switch, and delete branches in less than a second! It doesn’t matter if your repository is 1 file or 10, 000 files.

What

Before introducing Git branching, you need to know a few concepts:

Master branch: the master branch, which is automatically created when the GIt repository is initialized. It is used to deploy the production environment and must be stable.

What else do we need to do after we finish developing on the branch? Another concept, merge, is simply to merge the code of a branch into an existing branch, but conflict may occur, and the conflict needs to be resolved manually before committing.

In addition, like a warehouse, branches also have the concepts of local branches and remote branches. Simple understanding is that the local branch can only be visible by itself, while the remote branch can be pulled and cloned. We need to ** push ** to the remote branch to be visible to others.

How

Documentation on Git branch management can be found at: Official Documentation

See the branch

$ git branch     # View all local branches
$ git branch -a  # view all branches (local + remote)
$ git branch -r  View all remote branches
Copy the code

The current branch is preceded by an asterisk

Create & switch branches

$ git branch <newbranch>        Create a new branch and leave it in the current branch
$ git checkout <oldbranch>      # Switch branches (branches need to exist)
$ git checkout -b <newbranch>   Create a new branch and switch to the new branch
$ git fetch origin <branchname>:<branchname>  # pull from remote branch to local
Copy the code

New swtich command, see below (from Liao Xuemong — Git tutorial, I have not tried)

Git checkout —

is the same command as git checkout —

.

Actually, switching branches is more scientific with switch. Therefore, the latest version of Git provides a new Git switch command to switch branches:

To create and switch to a new dev branch, use:

$ git switch -c dev
Copy the code

To switch directly to the existing master branch, you can use:

$ git switch master
Copy the code

Using the new Git switch command is much easier to understand than git checkout.

Note: After creating a local branch, if you want to push it to a remote, you need to establish a trace with the remote upstream. The command reference is as follows:

$ git push --set-upstream origin <branchname> 	Push current branch and establish trace with remote upstream
Copy the code

Merging branches

$ git merge <branchname>	Merge a branch into the current branch
Copy the code

Merging branches can cause conflicts, which are beyond the scope of this article

Delete the branch

$ git branch -d <branchname>    # delete local branch (cut out)
$ git push origin --delete <branchname>  # delete remote branch
Copy the code

Summary

Common branch operation commands are summarized as follows:

$ git branch -a            # view all branches (local + remote)
$ git branch <newbranch>   Create a new branch and leave it in the current branch
$ git checkout <oldbranch> # Switch branches (branches need to exist)
$ git fetch origin <branchname>:<branchname>  # pull from remote branch to local
$ git push --set-upstream origin <branchname> Push current branch and establish trace with remote upstream
$ git merge <branchname>   Merge a branch into the current branch
$ git branch -d <branchname>            # delete local branch (cut out)
$ git push origin --delete <branchname> # delete remote branch
Copy the code

P.S. Guidelines for branch management and commit can be found in the Git branch development guidelines you must know