In general, our Git repository will have multiple branches. The main branch is called the master. In my opinion, most of the time, the rest of the branches are used to protect the main branch from inadvertent damage.

1. What is a branch

As you know, every time git executes the commit command, it saves the current file state as a snapshot in the repository.

The purpose of branching is to branch the overall process of changing records, and each branch is not affected by the other branches.

For example, three commit operations are performed on a repository, as shown in the following figure:

The black rectangle represents the snapshot generated by each commit, the blue rounded rectangle represents the branch, and the purple ellipse represents the header pointer.

This repository has only one master branch, and the header pointer points to the branch we are currently working on.

2. Create a branch

There are two commands to create a branch.

Create a new branch but still use the current branch:

Git Branch New branch nameCopy the code

Create a branch and switch to the new branch:

Git checkout -b branch nameCopy the code

For example, create branch dev:

git branch dev
Copy the code

The warehouse would then look like this:

New branches are typically created from the latest snapshot.

3. Switch branches

With the aforementioned checkout-b branch name, you can create a new branch and switch to the new branch, which is easy to switch to:

Git Checkout branch nameCopy the code

Use the checkout command.

Again, switch to dev branch:

The current branch is switched to Dev.

Then we do the development and do a commit, and what happens? The diagram below:

4. Merge branches

You can use the following command to fuse the specified branch to the current branch:

Git merge specifies the merge branchCopy the code

If you want to merge a version of the dev branch with the master branch, switch to the master branch and use the merge command:

git checkout master
git merge dev
Copy the code

The above warehouses are merged as follows:

5, view the current warehouse branch

This is easy.

View the local branch of the current repository:

git branch
Copy the code

View the current repository remote branch:

git branch -r
Copy the code

View all branches of the current repository (local + remote) :

git branch -a
Copy the code

6. Establish tracing relationship between local branch and remote branch

By default, only the master branch of the repository is cloned. But what about developing an existing dev branch of the remote repository? In fact, there are many methods, here to explain one by one.

(1) Specify a branch when cloning

Git clone -b = -b git clone -b = -b

Git clone -b dev Repository addressCopy the code

(2) Create a local branchdevAnd the remote warehousedevBranches establish tracing relationships

For example, for a repository I just cloned, first look at all branches:

git branch -a
Copy the code

The local branch has only one master branch, but the remote branch has a dev branch in addition to the master branch.

Create a new branch and trace it to a remote branch using the git branch command:

git branch dev
git branch --set-upstream-to=remotes/origin/dev dev
Copy the code

The important thing is that this command sets up a tracing relationship between the local branch and the remote branch:

Git branch -- set-ups-to = Remote branch name local branch nameCopy the code

Git push XXX dev commits the branch change to the remote repository, and XXX is changed to its own repository alias.

An even simpler approach is to use the checkout command to create a local branch, switch to the branch, and trace the branch to the specified remote branch:

Git checkout -b Local branch name Remote branch nameCopy the code

For example, once the warehouse is cloned, we only need to implement this sentence:

git checkout -b dev remotes/origin/dev
Copy the code

This command creates a local dev branch, switches to it, and sets up a tracing relationship with the remote dev branch.

The git branch — set-ups-to = command is used to manually set up the tracing relationship between the existing local branch and the remote branch, while the git checkout -b command is used to create, switch, and trace the existing branch in one step. These two commands are used depending on the situation.

7. Delete the branch

Deleting a branch is simple, but dangerous, so use it with caution!

Delete a local branch:

Git branch -d Specifies the name of a branchCopy the code

Delete remote branch:

Git branch - Dr Specifies the name of a remote branchCopy the code