用法1:git branch
用法2:git branch <branchname>
用法3:git branch <branchname> <start-point>
用法4:git branch -d <branchname>
用法5:git branch -D <branchname>
用法6:git branch -m <oldbranchname> <newbranch>
用法7:git branch -M <oldbranch> <newbranch>
用法1:查看本地分值列表 带*的是当前分支
用法2和用法3创建分支,用法2是基于HEAD指向的提交创建分支<branchname> 用法3是基于提交<start-point>创建分支<branchname>
用法4和用法5用于删除分支。用法4删除分支<branchname>会检查将被删除的分支是否合并在其他分支,如果有会拒绝删除,用法5会强制删除
用法6和用法7用于重命名分支。如果版本库中已经存在名为<newbranch>的命名,用法6会拒绝执行。用法7强制执行
创建分支git branch <branchname> 切换分支 git checkout <branchname>
创建分支+切换分支git checkout -b <branchname>== git branch <branchname> + git checkout <branchname>
遗留:由于公司gerrit的tag权限没有放开gerrit push tag报错:prohibited by Gerrit: create not permitted for refs/tags/v1.0 需要经过code-review,按照自己思路模拟书上的内容
本地仓库:
2个用户
jagent@ubuntu:~/xin$ git clone ssh://xwx496887@10.148.192.216:29418/ST/mytest user1
Cloning into 'user1'...
remote: Counting objects: 48, done
remote: Finding sources: 100% (48/48)
remote: Total 48 (delta 11), reused 45 (delta 11)
Receiving objects: 100% (48/48), 4.56 KiB | 0 bytes/s, done.
Resolving deltas: 100% (11/11), done.
Checking connectivity... done.
jagent@ubuntu:~/xin$ git clone ssh://xwx496887@10.148.192.216:29418/ST/mytest user2
Cloning into 'user2'...
remote: Counting objects: 48, done
remote: Finding sources: 100% (48/48)
remote: Total 48 (delta 11), reused 45 (delta 11)
Receiving objects: 100% (48/48), 4.56 KiB | 0 bytes/s, done.
Resolving deltas: 100% (11/11), done.
Checking connectivity... done.
jagent@ubuntu:~/xin$ cd user1;git config user.name user1 ;git config user.email user1@126.com
jagent@ubuntu:~/xin$ cd user2;git config user.name user2 ;git config user.email user2@126.com
基于特性分支开发
user1的功能为openuser1.txt user2的功能为openuser2.txt
jagent@ubuntu:~/xin/user1$ git branch
* master
jagent@ubuntu:~/xin/user1$ git checkout -b user1/openuser1
Switched to a new branch 'user1/openuser1'
jagent@ubuntu:~/xin/user1$ git branch
master
* user1/openuser1
此时引用指向master 和user1/openuser1分支相同
touch openuser1.txt;echo "finished user1 open" >>openuser1.txt
jagent@ubuntu:~/xin/user1$ git add -A;git commit -m "user1 finished"
[user1/openuser1 5270a81] user1 finished
1 file changed, 1 insertion(+)
create mode 100644 openuser1.txt
将特性分支合并在主干master
jagent@ubuntu:~/xin/user1$ git merge user1/openuser1
Updating 272ee08..5270a81
Fast-forward
openuser1.txt | 1 +
1 file changed, 1 insertion(+)
create mode 100644 openuser1.txt
合入分支时,在实际开发中,可能别人也又合入可以
git cherry查看,最好git pull合并下,有冲突解决冲突
jagent@ubuntu:~/xin/user1$ git push origin master
Counting objects: 10, done.
Delta compression using up to 48 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 277 bytes | 0 bytes/s, done.
Total 3 (delta 1), reused 0 (delta 0)
remote: Resolving deltas: 100% (1/1)
remote: Processing changes: done
To ssh://xwx496887@10.148.192.216:29418/ST/mytest
272ee08..5270a81 master -> master
同理 user2的特性分支也是这样操作。
发布分支的开发。
在发生bug的历史提交的某一个节点中拉去分支,不影响主干开发
当修复bug修复后,将修复提交git cherrypick到主干分支即可
分支变基操作
合并分支推送(顺序不一致)
git rebase master:分支
变基前:
master
|
V
A-B-C-D-E-F-H
|
v
J-K-L-M-N
^
|
i18n
变基后:
master
|
V
A-B-C-D-E-F-H-G'-G''