什么是分支
软件项目中启动一套单独的开发线的方法
为什么使用 Git
可以很好的避免版本兼容开发的问题, 避免不同版本之间的相互影响.
封装一个开发阶段.
解决 bug 的时候新建分支, 用于对该 bug 的研究.
Git 中跟分支相关的命令
Git branch 分支名 --- 创建一个新的分支
Git branch --- 不加任何参数, 列出所有的分支, 分支前面有 * 号, 代表该分支为当前所在分支
* 创建分支的时候, 分支名不能使用特殊符号
Git branch -d 分支名 --- 删除分支
Git branch -m 旧分支名 新分支名 --- 分支名改名
Git checkout 分支名 --- 切换分支, 如果在分支上面对文件进行修改之后, 没有 commit 就切换到另外一个支支
这个时候会报错, 因为没有 commit 的文件在切换分支之后会不覆盖
Git checkout -f 分支名 --- 强制切换分支.
Git 分支展示
- kangdeMacBook-Air:cedarhd kang$ Git branch #查看当所所有分支
- * master
- kangdeMacBook-Air:cedarhd kang$ Git branch v1.0 #新建一个 v1.0 的分支
- kangdeMacBook-Air:cedarhd kang$ Git branch
- * master
- v1.0
- kangdeMacBook-Air:cedarhd kang$ Git checkout v1.0 #切换到 v1.0 的分支
- Switched to branch 'v1.0'
- kangdeMacBook-Air:cedarhd kang$ Git branch
- master
- * v1.0
- kangdeMacBook-Air:cedarhd kang$ touch test9.txt #在 v1.0 的分支上创建 test9.txt
- kangdeMacBook-Air:cedarhd kang$ ls -al
- total 32
- drwxr-xr-x 13 kang staff 442 12 2 00:12 .
- drwxr-xr-x 5 kang staff 170 12 1 16:48 ..
- drwxr-xr-x 16 kang staff 544 12 2 00:12 .Git
- -rw-r--r-- 1 kang staff 3 12 1 16:18 test.txt
- -rw-r--r-- 1 kang staff 23 12 1 16:18 test1.txt
- -rw-r--r-- 1 kang staff 0 12 1 16:18 test2.txt
- -rw-r--r-- 1 kang staff 6 12 1 16:18 test3.txt
- -rw-r--r-- 1 kang staff 17 12 1 16:18 test4.txt
- -rw-r--r-- 1 kang staff 0 12 1 16:21 test5.txt
- -rw-r--r-- 1 kang staff 0 12 1 16:37 test6.txt
- -rw-r--r-- 1 kang staff 0 12 1 22:43 test7.txt
- -rw-r--r-- 1 kang staff 0 12 1 23:15 test8.txt
- -rw-r--r-- 1 kang staff 0 12 2 00:12 test9.txt
- kangdeMacBook-Air:cedarhd kang$ Git add test9.txt #提交到暂存区
- kangdeMacBook-Air:cedarhd kang$ Git commit -m "test9.txt" #提交到本地仓库
- [v1.0 6111bf3] test9.txt
- Committer: kang <kang@kangdeMacBook-Air.local>
- Your name and email address were configured automatically based
- on your username and hostname. Please check that they are accurate.
- You can suppress this message by setting them explicitly. Run the
- following command and follow the instructions in your editor to edit
- your configuration file:
- Git config --global --edit
- After doing this, you may fix the identity used for this commit with:
- Git commit --amend --reset-author
- 1 file changed, 0 insertions(+), 0 deletions(-)
- create mode 100644 test9.txt
- kangdeMacBook-Air:cedarhd kang$ ls -al
- total 32
- drwxr-xr-x 13 kang staff 442 12 2 00:12 .
- drwxr-xr-x 5 kang staff 170 12 1 16:48 ..
- drwxr-xr-x 16 kang staff 544 12 2 00:12 .Git
- -rw-r--r-- 1 kang staff 3 12 1 16:18 test.txt
- -rw-r--r-- 1 kang staff 23 12 1 16:18 test1.txt
- -rw-r--r-- 1 kang staff 0 12 1 16:18 test2.txt
- -rw-r--r-- 1 kang staff 6 12 1 16:18 test3.txt
- -rw-r--r-- 1 kang staff 17 12 1 16:18 test4.txt
- -rw-r--r-- 1 kang staff 0 12 1 16:21 test5.txt
- -rw-r--r-- 1 kang staff 0 12 1 16:37 test6.txt
- -rw-r--r-- 1 kang staff 0 12 1 22:43 test7.txt
- -rw-r--r-- 1 kang staff 0 12 1 23:15 test8.txt
- -rw-r--r-- 1 kang staff 0 12 2 00:12 test9.txt
- kangdeMacBook-Air:cedarhd kang$ Git branch
- * master
- v1.0
- kangdeMacBook-Air:cedarhd kang$ Git checkout master #切换到 master 分支
- Switched to branch 'master'
- Your branch is ahead of 'origin/master' by 1 commit.
- (use "git push" to publish your local commits)
- kangdeMacBook-Air:cedarhd kang$ ls -al #此时 v1.0 的 test9.txt 不见了
- total 32
- drwxr-xr-x 12 kang staff 408 12 2 00:12 .
- drwxr-xr-x 5 kang staff 170 12 1 16:48 ..
- drwxr-xr-x 16 kang staff 544 12 2 00:12 .Git
- -rw-r--r-- 1 kang staff 3 12 1 16:18 test.txt
- -rw-r--r-- 1 kang staff 23 12 1 16:18 test1.txt
- -rw-r--r-- 1 kang staff 0 12 1 16:18 test2.txt
- -rw-r--r-- 1 kang staff 6 12 1 16:18 test3.txt
- -rw-r--r-- 1 kang staff 17 12 1 16:18 test4.txt
- -rw-r--r-- 1 kang staff 0 12 1 16:21 test5.txt
- -rw-r--r-- 1 kang staff 0 12 1 16:37 test6.txt
- -rw-r--r-- 1 kang staff 0 12 1 22:43 test7.txt
- -rw-r--r-- 1 kang staff 0 12 1 23:15 test8.txt
- kangdeMacBook-Air:cedarhd kang$ Git checkout v1.0 #切换到 v1.0 分支
- Switched to branch 'v1.0'
- kangdeMacBook-Air:cedarhd kang$ ls #test9.txt 存在
- test.txt test2.txt test4.txt test6.txt test8.txt
- test1.txt test3.txt test5.txt test7.txt test9.txt
来源: http://www.bubuko.com/infodetail-2869336.html