Git branch mechanism is an important part of the code versioning. We can create new branches in order to make the same source available multiple development paths. In this tutorial, we will learn how to create and manage a new branch with Git.
Git分支机制是代码版本控制的重要组成部分。 我们可以创建新分支,以使同一源可用于多个开发路径。 在本教程中,我们将学习如何使用Git创建和管理新分支。
列出分支 (List Branches )
We will start by listing the current existing branches. We will use git branch
command without any option or parameter. The branch which prefixed with *
is the current working or active branch.
我们将从列出现有分支机构开始。 我们将使用不带任何选项或参数的git branch
命令。 带有*
前缀的分支是当前工作或活动分支。
$ git branch

创建本地分支并切换此分支(Create Local Branch and Switch This Branch)
We can create a local branch with different commands branch
and checkout
. We will use branch
command and provide the branch name test
in this example.
我们可以使用不同的命令branch
和checkout
创建一个本地分支。 在此示例中,我们将使用branch
命令并提供分支名称test
。
$ git branch test
We will use checkout
command with -b
option and branch name. This will also automatically change the current branch to the newly created branch. In this example, we will create a branch named silver
.
我们将使用带有-b
选项和分支名称的checkout
命令。 这还将自动将当前分支更改为新创建的分支。 在此示例中,我们将创建一个名为silver
的分支。
$ git checkout -b silver
We can see the message Switched to a new branch 'silver'
.
我们可以看到消息Switched to a new branch 'silver'
。
变更工作处 (Change Working Branch)
If we just want to change the current working or active branch we can use the checkout
command. We will also provide the branch name we want to change. In this example, we will change the branch newversion
.
如果我们只想更改当前的工作分支或活动分支,则可以使用checkout
命令。 我们还将提供我们想要更改的分支名称。 在此示例中,我们将更改分支newversion
。
$ git checkout newversion
为当前分支添加新的远程存储库 (Add New Remote Repository For Current Branch)
Git is mainly designed to be used in a distributed manner. So there will be a lot of remote Git repositories and branches. We can add a new remote branch for the current branch with the remote add
command. In this example, we will add remote system github.com new branch named test
Git主要设计为以分布式方式使用。 因此,将会有很多远程Git存储库和分支。 我们可以使用remote add
命令为当前分支添加一个新的远程分支。 在这个例子中,我们将添加远程系统github.com名为test
新分支。
$ git remote add github.com/ibaydan test
从远程存储库更新分支 (Update Branch From Remote Repository)
Git remote branches can be updates frequently where our local branch may be outdated. We can update our local branch from the remote repository branch with the fetch
command like below. We will update the local branch from remote named github.com/ibaydan
Git远程分支可以经常更新,而我们的本地分支可能已过时。 我们可以使用如下所示的fetch
命令从远程存储库分支更新本地分支。 我们将从名为github.com/ibaydan
远程更新本地分支
$ git fetch github.com/ibaydan
删除本地分支 (Delete Local Branch)
After completing changes and merged to the main branch we may need to delete the local branch. we will use branch command with -d option. -d means delete. We will delete the local branch named silver
in this example.
完成更改并合并到主分支后,我们可能需要删除本地分支。 我们将使用带有-d选项的branch命令。 -d表示删除。 在此示例中,我们将删除名为silver
的本地分支。
$ git branch -d silver
强制删除本地分支 (Force Delete Local Branch)
If there are some problems for normal removal or deletion of the local branch we can force delete operation. We will use -D
option for forcing deletion. In this example we will delete branch named silver
如果正常删除或删除本地分支存在一些问题,我们可以强制执行删除操作。 我们将使用-D
选项强制删除。 在此示例中,我们将删除名为silver
分支
$ git branch -D silver
翻译自: https://www.poftut.com/how-to-create-and-manage-new-branch-with-git/