1. git checkout -
b - 创建并切换分支
如果想以当前的 master 分支为底子创建新的分支,我们须要用到 git checkout -
b 命令。切换到 feature-A 分支并举行提交,执行下面的命令,创建名为 feature-A 的分支。
- git checkout -
- b <new-branch-name>
- 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 分支下。
- strong@foreverstrong:~/github_work/git-tutorial$ git branch
- * feature-A masterstrong@foreverstrong:~/github_work/git-tutorial$
复制代码 feature-A 分支左侧标有 *,表现当前分支为 feature-A。在这个状态下像正常开发那样修改代码、执行 git add 命令并举行提交的话,代码就会提交至 feature-A 分支。像如许不停对一个分支 (例如 feature-A) 举行提交的操作,我们称为培养分支。
下面来实际操作一下。在 README.md 文件中添加一行。
这里我们添加了 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 <branch-name>
- git checkout master
- git checkout main
复制代码- strong@foreverstrong:~/github_work/git-tutorial$ git checkout masterSwitched to branch 'master'strong@foreverstrong:~/github_work/git-tutorial$ strong@foreverstrong:~/github_work/git-tutorial$ git branch
- feature-A* masterstrong@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 分支。
- strong@foreverstrong:~/github_work/git-tutorial$ git branch
- feature-A* masterstrong@foreverstrong:~/github_work/git-tutorial$ git checkout -
- Switched to branch 'feature-A'strong@foreverstrong:~/github_work/git-tutorial$ git branch
- * feature-A masterstrong@foreverstrong:~/github_work/git-tutorial$ strong@foreverstrong:~/github_work/git-tutorial$ git checkout masterSwitched to branch 'master'strong@foreverstrong:~/github_work/git-tutorial$ git branch
- feature-A* masterstrong@foreverstrong:~/github_work/git-tutorial$ git checkout feature-A Switched to branch 'feature-A'strong@foreverstrong:~/github_work/git-tutorial$ git branch
- * feature-A masterstrong@foreverstrong:~/github_work/git-tutorial$
复制代码 像上面如许用 - (连字符) 代替分支名,就可以切换至上一个分支。固然,将 - 更换成 feature-A 同样可以切换到 feature-A 分支。
4. 切换到标签 (tag)
使用 git tag
命令检察所有的 tags 列表,确认你要切换的 tag 存在。
使用 git checkout 命令加上 tag 的名称切换
- git checkout tags/<tag-name>
复制代码 References
[1] Yongqiang Cheng, https://yongqiang.blog.csdn.net/
[2] (日) 大塚弘记 著, 支鹏浩, 刘斌 译. GitHub入门与实践[M]. 北京:人民邮电出版社, 2015. 1-255
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |