
98ca9,34ac2,f30ab分别为前3次提交内容,都有快照,工作在master分支上,master上面有一个HEAD指针指向它,所以每次提交都提交到master上面来

开发一个新的功能时,不能在master上开发,要在一个新的分支上开发,不然会很乱,比如testing分支

创建新分支后,HEAD指针还是指向master上,这时需要切换一下分支

切换分支后,HEAD指针指向testing上了

示例:
[root@localhost test]# git branch about ---要开发一个about模块,所以创建一个名为about的分支
[root@localhost test]# git status
On branch master ---创建分支后还在master上
nothing to commit, working directory clean
[root@localhost test]# git checkout about ---切换指针
Switched to branch 'about'
[root@localhost test]# git status
On branch about ---工作在about上了
nothing to commit, working directory clean
[root@localhost test]# git log ---创建新的分支后还是只有前面的3个提交记录
commit 8880be9fc12c00fe8f5a26a5f87e6b6af193ac7b
Author: zheng <zheng@qq.com>
Date: Sat Mar 2 20:43:44 2019 +0800
test
commit 8ce4e28510f0bdf1153c064e507b11e2b2052744
Author: zheng <zheng@qq.com>
Date: Sat Mar 2 20:41:12 2019 +0800
news
commit b613f505626d37c74b262ba5a345ef41740785fb
Author: zheng <zheng@qq.com>
Date: Sat Mar 2 20:33:08 2019 +0800
first commit
[root@localhost test]# ll
total 12
-rw-r--r--. 1 root root 21 Mar 2 20:24 index.html
-rw-r--r--. 1 root root 5 Mar 2 20:38 news.html
-rw-r--r--. 1 root root 22 Mar 2 20:43 test.html
[root@localhost test]# vi about.html ---开发一个新模块
about us
:wq
[root@localhost test]# git log ---有4次提交记录了
commit 06c81edf9d8db4ab95c1558f8a551ac81de5a659
Author: zheng <zheng@qq.com>
Date: Sat Mar 2 21:40:40 2019 +0800
about
commit 8880be9fc12c00fe8f5a26a5f87e6b6af193ac7b
Author: zheng <zheng@qq.com>
Date: Sat Mar 2 20:43:44 2019 +0800
test
commit 8ce4e28510f0bdf1153c064e507b11e2b2052744
Author: zheng <zheng@qq.com>
Date: Sat Mar 2 20:41:12 2019 +0800
news
commit b613f505626d37c74b262ba5a345ef41740785fb
Author: zheng <zheng@qq.com>
Date: Sat Mar 2 20:33:08 2019 +0800
first commit
[root@localhost test]# ll
total 16
-rw-r--r--. 1 root root 9 Mar 2 21:40 about.html
-rw-r--r--. 1 root root 21 Mar 2 20:24 index.html
-rw-r--r--. 1 root root 5 Mar 2 20:38 news.html
-rw-r--r--. 1 root root 22 Mar 2 20:43 test.html
[root@localhost test]# git checkout master ---切换回master
Switched to branch 'master'
[root@localhost test]# git log ---发现只能看到之前的3个提交,看不到about的提交,说明分支已经朝着不同的方向演进了
commit 8880be9fc12c00fe8f5a26a5f87e6b6af193ac7b
Author: zheng <zheng@qq.com>
Date: Sat Mar 2 20:43:44 2019 +0800
test
commit 8ce4e28510f0bdf1153c064e507b11e2b2052744
Author: zheng <zheng@qq.com>
Date: Sat Mar 2 20:41:12 2019 +0800
news
commit b613f505626d37c74b262ba5a345ef41740785fb
Author: zheng <zheng@qq.com>
Date: Sat Mar 2 20:33:08 2019 +0800
first commit
[root@localhost test]# git branch ---可以查看当前所在分支为master
about
* master
[root@localhost test]# git checkout about
Switched to branch 'about'
[root@localhost test]# git branch
* about
master

示例2:
把分支上的代码挪到master上去
[root@localhost test]# vi about2.html
about2
:wq
[root@localhost test]# git add .
[root@localhost test]# git commit -m "about2"
[about 96c9ee3] about2
1 file changed, 1 insertion(+)
create mode 100644 about2.html
[root@localhost test]# git log ---一共5次提交
commit 96c9ee3183d8589922448da51a66554037f4c76b
Author: zheng <zheng@qq.com>
Date: Sat Mar 2 22:16:16 2019 +0800
about2
commit 06c81edf9d8db4ab95c1558f8a551ac81de5a659
Author: zheng <zheng@qq.com>
Date: Sat Mar 2 21:40:40 2019 +0800
about
commit 8880be9fc12c00fe8f5a26a5f87e6b6af193ac7b
Author: zheng <zheng@qq.com>
Date: Sat Mar 2 20:43:44 2019 +0800
test
commit 8ce4e28510f0bdf1153c064e507b11e2b2052744
Author: zheng <zheng@qq.com>
Date: Sat Mar 2 20:41:12 2019 +0800
news
commit b613f505626d37c74b262ba5a345ef41740785fb
Author: zheng <zheng@qq.com>
Date: Sat Mar 2 20:33:08 2019 +0800
first commit
[root@localhost test]# git checkout master ---切换到master
Switched to branch 'master'
[root@localhost test]# git log
commit 8880be9fc12c00fe8f5a26a5f87e6b6af193ac7b
Author: zheng <zheng@qq.com>
Date: Sat Mar 2 20:43:44 2019 +0800
test
commit 8ce4e28510f0bdf1153c064e507b11e2b2052744
Author: zheng <zheng@qq.com>
Date: Sat Mar 2 20:41:12 2019 +0800
news
commit b613f505626d37c74b262ba5a345ef41740785fb
Author: zheng <zheng@qq.com>
Date: Sat Mar 2 20:33:08 2019 +0800
first commit
[root@localhost test]# git merge about ---把about分支与master共同的节点之后分离的节点重现在master分支上
Updating 8880be9..96c9ee3
Fast-forward
about.html | 1 +
about2.html | 1 +
2 files changed, 2 insertions(+)
create mode 100644 about.html
create mode 100644 about2.html
[root@localhost test]# git log ---master也有5次提交了
commit 96c9ee3183d8589922448da51a66554037f4c76b
Author: zheng <zheng@qq.com>
Date: Sat Mar 2 22:16:16 2019 +0800
about2
commit 06c81edf9d8db4ab95c1558f8a551ac81de5a659
Author: zheng <zheng@qq.com>
Date: Sat Mar 2 21:40:40 2019 +0800
about
commit 8880be9fc12c00fe8f5a26a5f87e6b6af193ac7b
Author: zheng <zheng@qq.com>
Date: Sat Mar 2 20:43:44 2019 +0800
test
commit 8ce4e28510f0bdf1153c064e507b11e2b2052744
Author: zheng <zheng@qq.com>
Date: Sat Mar 2 20:41:12 2019 +0800
news
commit b613f505626d37c74b262ba5a345ef41740785fb
Author: zheng <zheng@qq.com>
Date: Sat Mar 2 20:33:08 2019 +0800
first commit
[root@localhost test]# git branch --merged ---查看哪些分支已被并入当前分支
about
* master
[root@localhost test]# git branch --no-merged ---查看哪些分支没有被并入当前分支
网友评论