git系列文章目录
第一章 什么分支和git分支文章目录
前言
工作中不可避免的会遇到分工合作,如果某些功能交给新手开发,难免会出问题,这样的话为了不影响工作进度,就需要分工合作,等某部分完全稳定的时候再添加到主功能中,这样才是比较稳妥的方法一、分支是什么?
分支是git分工完成任务的一种团队协作方式,每一个功能都创建一个分支的最主要作用是为了避免相关功能污染主分支。
设想一下啊,如果某个软件需要多个测试功能不能稳定运行,那么也没有必要和主分支合并,这样就可以只合并稳定的功能然后迭代
二、使用步骤
1.引入新文件(工作中实现某部分新的功能这里只添加文本文件演示)
如下(示例):先新建一个test4.txt
Apple@DESKTOP-ECLNIU2 MINGW64 ~/Desktop/git basic operation/git-basic (master)
$ ll
total 25
-rw-r--r-- 1 Apple 197121 110 Jan 22 16:38 GitBasic.add
-rw-r--r-- 1 Apple 197121 9719 Jan 22 16:38 LICENSE
-rw-r--r-- 1 Apple 197121 45 Jan 22 17:17 OtherUser.add
-rw-r--r-- 1 Apple 197121 863 Jan 22 16:38 README.en.md
-rw-r--r-- 1 Apple 197121 1001 Jan 22 16:38 README.md
-rw-r--r-- 1 Apple 197121 0 Jan 22 17:23 add1.txt
-rw-r--r-- 1 Apple 197121 0 Jan 22 17:23 add2.txt
-rw-r--r-- 1 Apple 197121 0 Jan 22 17:23 add3.txt
-rw-r--r-- 1 Apple 197121 0 Jan 22 17:23 add4.txt
-rw-r--r-- 1 Apple 197121 61 Jan 22 17:55 test.txt
-rw-r--r-- 1 Apple 197121 0 Jan 22 18:52 test2.txt
-rw-r--r-- 1 Apple 197121 22 Jan 22 19:28 test3.txt
-rw-r--r-- 1 Apple 197121 17 Jan 22 19:11 test4.txt
Apple@DESKTOP-ECLNIU2 MINGW64 ~/Desktop/git basic operation/git-basic (master)
$ cat test4.txt
aaabbb
cccc
ddd
2.添加到本地仓库
代码如下(示例):
Apple@DESKTOP-ECLNIU2 MINGW64 ~/Desktop/git basic operation/git-basic (master)
$ git add test4.txt
Apple@DESKTOP-ECLNIU2 MINGW64 ~/Desktop/git basic operation/git-basic (master)
$ git commit -m "创建添加了test4.txt,用来演示分支" test4.txt
[master c2b6b4c] 创建添加了test4.txt,用来演示分支
1 file changed, 2 insertions(+), 1 deletion(-)
Apple@DESKTOP-ECLNIU2 MINGW64 ~/Desktop/git basic operation/git-basic (master)
$ git status
On branch master
Your branch is ahead of 'origin/master' by 14 commits.
(use "git push" to publish your local commits)
nothing to commit, working tree clean
Apple@DESKTOP-ECLNIU2 MINGW64 ~/Desktop/git basic operation/git-basic (master)
$
该处已经将test4.txt提交到本地仓库。
2.查看本地所有分支
目前只有一条主分支master使用命令查看
Apple@DESKTOP-ECLNIU2 MINGW64 ~/Desktop/git basic operation/git-basic (master)
$ git branch -v
* master c2b6b4c [ahead 14] 创建添加了test4.txt,用来演示分支
Apple@DESKTOP-ECLNIU2 MINGW64 ~/Desktop/git basic operation/git-basic (master)
$ git reflog
c2b6b4c (HEAD -> master) HEAD@{0}: commit: 创建添加了test4.txt,用来演示分支
0aa8a43 HEAD@{1}: commit: 更改test3演示了暂存区和本地库比较差异,结束后提交到本地库
a163bda HEAD@{2}: commit: 增加了一行空格和一行eee
e61828e HEAD@{3}: commit: 添加了一个test4.txt,并提交到本地库
8c42001 HEAD@{4}: commit: 新建一个test3.txt,并添加本地库
064ffb3 HEAD@{5}: reset: moving to 064ffb3
064ffb3 HEAD@{6}: reset: moving to 064ffb3
b5dfe8a HEAD@{7}: reset: moving to HEAD
b5dfe8a HEAD@{8}: commit: 删除了test2.txt,并同步到本地库
064ffb3 HEAD@{9}: commit: 新建一个test2.txt并提交到本地库
aa79b89 HEAD@{10}: reset: moving to aa79b89
2634270 HEAD@{11}: reset: moving to 2634270
db2d825 HEAD@{12}: reset: moving to db2d825
aa79b89 HEAD@{13}: commit: 又增加了一行d
db2d825 HEAD@{14}: commit: 又增加了一行c
2634270 HEAD@{15}: commit: 又增加了一行b
9f010b9 HEAD@{16}: commit: 在test.txt中增加了一行字符串a
81b1df6 HEAD@{17}: commit: 增加了一个test.txt
1c75a36 HEAD@{18}: commit: 手动添加4个测试文件add1~4用于演示_reset
1215585 HEAD@{19}: commit: 修改OtherUser.add
6b65ae8 HEAD@{20}: commit: 使用另外一个用户clone仓库后添加文件并commit
32a5af7 (origin/master, origin/HEAD) HEAD@{21}: clone: from https://gitee.com/gitee309307194/git-basic.git
Apple@DESKTOP-ECLNIU2 MINGW64 ~/Desktop/git basic operation/git-basic (master)
$
3.创建本地分支
创建一个新的分支branch01
Apple@DESKTOP-ECLNIU2 MINGW64 ~/Desktop/git basic operation/git-basic (master)
$ git branch branch01
Apple@DESKTOP-ECLNIU2 MINGW64 ~/Desktop/git basic operation/git-basic (master)
$ git branch -v
branch01 c2b6b4c 创建添加了test4.txt,用来演示分支
* master c2b6b4c [ahead 14] 创建添加了test4.txt,用来演示分支
Apple@DESKTOP-ECLNIU2 MINGW64 ~/Desktop/git basic operation/git-basic (master)
$
注意:当前所在的分支前面有*,说明当前所在的分支是master分支
3.切换本地分支
Apple@DESKTOP-ECLNIU2 MINGW64 ~/Desktop/git basic operation/git-basic (master)
$ git checkout branch01
Switched to branch 'branch01'
Apple@DESKTOP-ECLNIU2 MINGW64 ~/Desktop/git basic operation/git-basic (branch01)
$ git branch -v
* branch01 c2b6b4c 创建添加了test4.txt,用来演示分支
master c2b6b4c [ahead 14] 创建添加了test4.txt,用来演示分支
Apple@DESKTOP-ECLNIU2 MINGW64 ~/Desktop/git basic operation/git-basic (branch01)
$
此时branch01前面有*,说明已经切换到branch01分支了。而且两个分支的版本号是一样的。工作中如果有新人的话,就可以让其在该分支后面完成相应的功能了