1. 从本地分支创建新分支
(Purpose: Whenever you need to create a new feature, fix a bug, or rewrite any of your code, it's a good idea to create a new branch so that none of your changes affect the "master" version of the code.)
当开发一个功能时间较长时,可以创建一个新分支,在新分支上操作避免影响到 master 分支的代码。最终开发完成再合并到 master 分支提交。
# 从master分支拉出一条新的分支,两种方式
# 1. 创建后跳转到新分支
git checkout -b <branch-name>
# 2. 创建后留在master分支
git branch <branch-name>
# 从指定分支拉出一条新的分支
git checkout -b <new-branch> <dev-branch>
2. 从远程仓库里拉取一条新分支
git checkout -b <local-branch-name> <origin/remote-branch-name>
3. 从提交历史(commit)中拉出一条分支
hash 值是指 commit 字段的值。
# 两种方式(一个创建后留在master分支,一个创建后自动切到新分支):
git branch <branch-name> <hash>
git checkout -b <branch-name> <hash>
例子:
$ git branch
* master
$ git branch commit-branch 735c5b4
$ git branch
commit-branch
* master
4. 从标签(Tag
) 中拉出一条新分支
# 两种方式(一个创建后留在master分支,一个创建后自动切到新分支):
git branch <branch-name> <tag>
git checkout -b <branch-name> <tag>
例子:
$ git branch tag-branch v0.4.12
$ git branch
tag-branch
* master