赞
踩
1. git checkout -b - 创建、切换分支
如果想以当前的 master 分支为基础创建新的分支,我们需要用到 git checkout -b 命令。切换到 feature-A 分支并进行提交,执行下面的命令,创建名为 feature-A 的分支。
git checkout -b feature-A
- strong@foreverstrong:~/github_work/git-tutorial$ git checkout -b feature-A
- Switched to a new branch 'feature-A'
- strong@foreverstrong:~/github_work/git-tutorial$
实际上,连续执行下面两条命令也能收到同样效果。
- git branch feature-A
- git checkout feature-A
创建 feature-A 分支,并将当前分支切换为 feature-A 分支。这时再来查看分支列表,会显示我们处于 feature-A 分支下。
git branch
- strong@foreverstrong:~/github_work/git-tutorial$ git branch
- * feature-A
- master
- strong@foreverstrong:~/github_work/git-tutorial$
feature-A 分支左侧标有 “*”,表示当前分支为 feature-A。在这个状态下像正常开发那样修改代码、执行 git add 命令并进行提交的话,代码就会提交至 feature-A 分支。像这样不断对一个分支 (例如 feature-A) 进行提交的操作,我们称为“培育分支”。
下面来实际操作一下。在 README.md 文件中添加一行。
# Git Tutorial
这里我们添加了 feature-A 这样一行字母,然后进行提交。
- git add README.md
- git commit -m "Add feature-A"
- strong@foreverstrong:~/github_work/git-tutorial$ git add README.md
- strong@foreverstrong:~/github_work/git-tutorial$
- strong@foreverstrong:~/github_work/git-tutorial$ git commit -m "Add feature-A"
- [feature-A 6df1569] Add feature-A
- 1 file changed, 1 insertion(+), 1 deletion(-)
- strong@foreverstrong:~/github_work/git-tutorial$
于是,这一行就添加到 feature-A 分支中了。
2. 切换到 master 分支
现在我们再来看一看 master 分支有没有受到影响。首先切换至 master 分支。
git checkout master
- strong@foreverstrong:~/github_work/git-tutorial$ git checkout master
- Switched to branch 'master'
- strong@foreverstrong:~/github_work/git-tutorial$
- strong@foreverstrong:~/github_work/git-tutorial$ git branch
- feature-A
- * master
- strong@foreverstrong:~/github_work/git-tutorial$
- strong@foreverstrong:~/github_work/git-tutorial$ cat README.md
- # Git Tutorial
- strong@foreverstrong:~/github_work/git-tutorial$
然后查看 README.md 文件,会发现 README.md 文件仍然保持原先的状态,并没有被添加文字。feature-A 分支的更改不会影响到 master 分支,这正是在开发中创建分支的优点。只要创建多个分支,就可以在不互相影响的情况下同时进行多个功能的开发。
3. 切换回上一个分支
我们再切换回 feature-A 分支。
git checkout -
strong@foreverstrong:~/github_work/git-tutorial$ git branch feature-A * master strong@foreverstrong:~/github_work/git-tutorial$ git checkout - Switched to branch 'feature-A' strong@foreverstrong:~/github_work/git-tutorial$ git branch * feature-A master strong@foreverstrong:~/github_work/git-tutorial$ strong@foreverstrong:~/github_work/git-tutorial$ git checkout master Switched to branch 'master' strong@foreverstrong:~/github_work/git-tutorial$ git branch feature-A * master strong@foreverstrong:~/github_work/git-tutorial$ git checkout feature-A Switched to branch 'feature-A' strong@foreverstrong:~/github_work/git-tutorial$ git branch * feature-A master strong@foreverstrong:~/github_work/git-tutorial$
像上面这样用 “-” (连字符) 代替分支名,就可以切换至上一个分支。当然,将 “-” 替换成 feature-A 同样可以切换到 feature-A 分支。
https://yongqiang.blog.csdn.net/
(日) 大塚弘记 著, 支鹏浩, 刘斌 译. GitHub入门与实践[M]. 北京:人民邮电出版社, 2015. 1-255
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。