当前位置:   article > 正文

分支的操作 - git checkout -b

git checkout -b

分支的操作 - git checkout -b

1. git checkout -b - 创建、切换分支

如果想以当前的 master 分支为基础创建新的分支,我们需要用到 git checkout -b 命令。切换到 feature-A 分支并进行提交,执行下面的命令,创建名为 feature-A 的分支。

git checkout -b feature-A
  1. strong@foreverstrong:~/github_work/git-tutorial$ git checkout -b feature-A
  2. Switched to a new branch 'feature-A'
  3. strong@foreverstrong:~/github_work/git-tutorial$

实际上,连续执行下面两条命令也能收到同样效果。

  1. git branch feature-A
  2. git checkout feature-A

创建 feature-A 分支,并将当前分支切换为 feature-A 分支。这时再来查看分支列表,会显示我们处于 feature-A 分支下。

git branch
  1. strong@foreverstrong:~/github_work/git-tutorial$ git branch
  2. * feature-A
  3. master
  4. strong@foreverstrong:~/github_work/git-tutorial$

feature-A 分支左侧标有 “*”,表示当前分支为 feature-A。在这个状态下像正常开发那样修改代码、执行 git add 命令并进行提交的话,代码就会提交至 feature-A 分支。像这样不断对一个分支 (例如 feature-A) 进行提交的操作,我们称为“培育分支”。

下面来实际操作一下。在 README.md 文件中添加一行。

# Git Tutorial

这里我们添加了 feature-A 这样一行字母,然后进行提交。

  1. git add README.md
  2. git commit -m "Add feature-A"
  1. strong@foreverstrong:~/github_work/git-tutorial$ git add README.md
  2. strong@foreverstrong:~/github_work/git-tutorial$
  3. strong@foreverstrong:~/github_work/git-tutorial$ git commit -m "Add feature-A"
  4. [feature-A 6df1569] Add feature-A
  5. 1 file changed, 1 insertion(+), 1 deletion(-)
  6. strong@foreverstrong:~/github_work/git-tutorial$

于是,这一行就添加到 feature-A 分支中了。

2. 切换到 master 分支

现在我们再来看一看 master 分支有没有受到影响。首先切换至 master 分支。

git checkout master
  1. strong@foreverstrong:~/github_work/git-tutorial$ git checkout master
  2. Switched to branch 'master'
  3. strong@foreverstrong:~/github_work/git-tutorial$
  4. strong@foreverstrong:~/github_work/git-tutorial$ git branch
  5. feature-A
  6. * master
  7. strong@foreverstrong:~/github_work/git-tutorial$
  8. strong@foreverstrong:~/github_work/git-tutorial$ cat README.md
  9. # Git Tutorial
  10. strong@foreverstrong:~/github_work/git-tutorial$

然后查看 README.md 文件,会发现 README.md 文件仍然保持原先的状态,并没有被添加文字。feature-A 分支的更改不会影响到 master 分支,这正是在开发中创建分支的优点。只要创建多个分支,就可以在不互相影响的情况下同时进行多个功能的开发。

3. 切换回上一个分支

我们再切换回 feature-A 分支。

git checkout -
  1. strong@foreverstrong:~/github_work/git-tutorial$ git branch
  2. feature-A
  3. * master
  4. strong@foreverstrong:~/github_work/git-tutorial$ git checkout -
  5. Switched to branch 'feature-A'
  6. strong@foreverstrong:~/github_work/git-tutorial$ git branch
  7. * feature-A
  8. master
  9. strong@foreverstrong:~/github_work/git-tutorial$
  10. strong@foreverstrong:~/github_work/git-tutorial$ git checkout master
  11. Switched to branch 'master'
  12. strong@foreverstrong:~/github_work/git-tutorial$ git branch
  13. feature-A
  14. * master
  15. strong@foreverstrong:~/github_work/git-tutorial$ git checkout feature-A
  16. Switched to branch 'feature-A'
  17. strong@foreverstrong:~/github_work/git-tutorial$ git branch
  18. * feature-A
  19. master
  20. strong@foreverstrong:~/github_work/git-tutorial$

像上面这样用 “-” (连字符) 代替分支名,就可以切换至上一个分支。当然,将 “-” 替换成 feature-A 同样可以切换到 feature-A 分支。

References

https://yongqiang.blog.csdn.net/

(日) 大塚弘记 著, 支鹏浩, 刘斌 译. GitHub入门与实践[M]. 北京:人民邮电出版社, 2015. 1-255

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/747035
推荐阅读
  

闽ICP备14008679号