1.创建分支
//创建分支,是在master 分支的基础上创建
[email protected]-MS-7817:~/myGit$ git st # On branch master nothing to commit (working directory clean) [email protected]-MS-7817:~/myGit$ ls aaa.txt [email protected]-MS-7817:~/myGit$ git branch second master [email protected]-MS-7817:~/myGit$ git br -av * master f73968d my second commit second f73968d my second commit
星号表示当前所处的分支,一般master是主分支
2.删除分支
[email protected]7817:~/myGit$ git br -av * master f73968d my second commit second f73968d my second commit [email protected]-MS-7817:~/myGit$ git branch -d master error: Cannot delete the branch ‘master‘ which you are currently on. [email protected]-MS-7817:~/myGit$ git branch -d second Deleted branch second (was f73968d). [email protected]-MS-7817:~/myGit$ git br -av * master f73968d my second commit [email protected]-MS-7817:~/myGit$
我们不能删除当前所处的分支
[email protected]7817:~/myGit$ git branch second master [email protected]-MS-7817:~/myGit$ git br -av * master f73968d my second commit second f73968d my second commit [email protected]-MS-7817:~/myGit$ git checkout second Switched to branch ‘second‘ [email protected]-MS-7817:~/myGit$ git br -av master f73968d my second commit * second f73968d my second commit [email protected]-MS-7817:~/myGit$ git branch -d master Deleted branch master (was f73968d). [email protected]-MS-7817:~/myGit$
事实证明,主分支也是可以删除的,这个似乎不存在所谓的父类节点关系,倒是更像 拷贝
3.探索两个分支间的关系
[email protected]7817:~/myGit$ git br -av * second 2e32a61 master add [email protected]-MS-7817:~/myGit$ git branch son second [email protected]-MS-7817:~/myGit$ git br -av * second 2e32a61 master add son 2e32a61 master add [email protected]-MS-7817:~/myGit$ git log commit 2e32a615216de70720d314b8fe53c1bddccfd6a3 Author: zhangshuli <[email protected]> Date: Thu Feb 26 16:55:33 2015 +0000 master add commit f73968df521e48d23289c5563f7ac7fbc5937b57 Author: zhangshuli <[email protected]> Date: Fri Feb 13 13:24:15 2015 +0000 my second commit commit 4c7cede87be37783ca4528fbdcd79bc08e3870fe Author: zhangshuli <[email protected]> Date: Fri Feb 13 10:54:58 2015 +0000 my frist commit [email protected]-MS-7817:~/myGit$ git checkout son Switched to branch ‘son‘ [email protected]-MS-7817:~/myGit$ git br -av second 2e32a61 master add * son 2e32a61 master add [email protected]-MS-7817:~/myGit$ git log commit 2e32a615216de70720d314b8fe53c1bddccfd6a3 Author: zhangshuli <[email protected]> Date: Thu Feb 26 16:55:33 2015 +0000 master add commit f73968df521e48d23289c5563f7ac7fbc5937b57 Author: zhangshuli <[email protected]> Date: Fri Feb 13 13:24:15 2015 +0000 my second commit commit 4c7cede87be37783ca4528fbdcd79bc08e3870fe Author: zhangshuli <[email protected]> Date: Fri Feb 13 10:54:58 2015 +0000 my frist commit [email protected]-MS-7817:~/myGit$
1)创建分支以后,两个分支的内容完全相同
[email protected]7817:~/myGit$ vim ccc.txt [email protected]-MS-7817:~/myGit$ git st # On branch son # Untracked files: # (use "git add <file>..." to include in what will be committed) # # ccc.txt nothing added to commit but untracked files present (use "git add" to track) [email protected]-MS-7817:~/myGit$ git commit # On branch son # Untracked files: # (use "git add <file>..." to include in what will be committed) # # ccc.txt nothing added to commit but untracked files present (use "git add" to track) [email protected]-MS-7817:~/myGit$ git add . [email protected]-MS-7817:~/myGit$ git st # On branch son # Changes to be committed: # (use "git reset HEAD <file>..." to unstage) # # new file: ccc.txt # [email protected]-MS-7817:~/myGit$ git checkout second A ccc.txt Switched to branch ‘second‘ [email protected]-MS-7817:~/myGit$ git log commit 2e32a615216de70720d314b8fe53c1bddccfd6a3 Author: zhangshuli <[email protected]> Date: Thu Feb 26 16:55:33 2015 +0000 master add commit f73968df521e48d23289c5563f7ac7fbc5937b57 Author: zhangshuli <[email protected]> Date: Fri Feb 13 13:24:15 2015 +0000 my second commit commit 4c7cede87be37783ca4528fbdcd79bc08e3870fe Author: zhangshuli <[email protected]> Date: Fri Feb 13 10:54:58 2015 +0000 my frist commit [email protected]-MS-7817:~/myGit$ ls aaa.txt bbb.txt ccc.txt [email protected]-MS-7817:~/myGit$
2)当我在子分支上添加了一个修改的时候,父分支会跟着做相应的修改
[email protected]7817:~/myGit$ git br -av * parent 5ad0d1e parent test son 2974d86 son test [email protected]-MS-7817:~/myGit$ ls aaa.txt bbb.txt ddd.txt [email protected]-MS-7817:~/myGit$ git log -1 commit 5ad0d1ece9b7fd69a9e4deb990c8a4d05d2cbee1 Author: zhangshuli <[email protected]> Date: Thu Feb 26 17:32:39 2015 +0000 parent test [email protected]-MS-7817:~/myGit$ git checkout son Switched to branch ‘son‘ [email protected]-MS-7817:~/myGit$ ls aaa.txt bbb.txt ccc.txt ddd.txt [email protected]-MS-7817:~/myGit$ git log -1 commit 2974d86cea7341e5921368ceb708c61f7c77e6c5 Author: zhangshuli <[email protected]> Date: Thu Feb 26 17:26:11 2015 +0000 son test [email protected]-MS-7817:~/myGit$ vim add.txt [email protected]-MS-7817:~/myGit$ vim commit.txt [email protected]-MS-7817:~/myGit$ git add add.txt [email protected]-MS-7817:~/myGit$ git add commit.txt [email protected]-MS-7817:~/myGit$ git commit commit.txt [son 17619d8] son commit 1 files changed, 1 insertions(+), 0 deletions(-) create mode 100644 commit.txt [email protected]-MS-7817:~/myGit$ ls aaa.txt add.txt bbb.txt ccc.txt commit.txt ddd.txt [email protected]-MS-7817:~/myGit$ git st # On branch son # Changes to be committed: # (use "git reset HEAD <file>..." to unstage) # # new file: add.txt # [email protected]-MS-7817:~/myGit$ git log -1 commit 17619d87e77a80ef744be6b542cd87ef2f62b71e Author: zhangshuli <[email protected]> Date: Thu Feb 26 17:36:32 2015 +0000 son commit [email protected]-MS-7817:~/myGit$ git checkout parent A add.txt Switched to branch ‘parent‘ [email protected]-MS-7817:~/myGit$ ls aaa.txt add.txt bbb.txt ddd.txt [email protected]-MS-7817:~/myGit$ ls aaa.txt add.txt bbb.txt ddd.txt [email protected]-MS-7817:~/myGit$ git st # On branch parent # Changes to be committed: # (use "git reset HEAD <file>..." to unstage) # # new file: add.txt # [email protected]-MS-7817:~/myGit$ git log -1 commit 5ad0d1ece9b7fd69a9e4deb990c8a4d05d2cbee1 Author: zhangshuli <[email protected]> Date: Thu Feb 26 17:32:39 2015 +0000 parent test [email protected]-MS-7817:~/myGit$
3)终于可以对分支盖棺定论了,原来是这样的,两个分支是同步的,直到其中的一个进行了commit,否则,当我们切换两个有父子关系的分支的时候,分支会自动merge,如果父分支创建了子分支已经存在的文件,你会发现会有merge冲突。如下
[email protected]7817:~/myGit$ git st # On branch parent # Changes to be committed: # (use "git reset HEAD <file>..." to unstage) # # new file: add.txt # # Changed but not updated: # (use "git add <file>..." to update what will be committed) # (use "git checkout -- <file>..." to discard changes in working directory) # # modified: aaa.txt # [email protected]-MS-7817:~/myGit$ git checkout son M aaa.txt A add.txt Switched to branch ‘son‘ [email protected]-MS-7817:~/myGit$ git st . # On branch son # Changes to be committed: # (use "git reset HEAD <file>..." to unstage) # # new file: add.txt # # Changed but not updated: # (use "git add <file>..." to update what will be committed) # (use "git checkout -- <file>..." to discard changes in working directory) # # modified: aaa.txt # [email protected]-MS-7817:~/myGit$ vim aaa.txt [email protected]-MS-7817:~/myGit$ vim commit.txt [email protected]-MS-7817:~/myGit$ git checkout parent M aaa.txt A add.txt Switched to branch ‘parent‘ [email protected]-MS-7817:~/myGit$ vim commit.txt [email protected]-MS-7817:~/myGit$ git checkout son error: Untracked working tree file ‘commit.txt‘ would be overwritten by merge. [email protected]-MS-7817:~/myGit$
如果我们把修改提交了,那么你就会发现两个分支是完全不关联的
时间: 2024-10-13 14:52:24