分布式版本控制系统之git分支管理及标签管理
创建分支
git branch [name]
git branch dev #创建dev分支
git branch #查看分支
[root@cool360 u02]# git branch
* dev
master
切换分支:
git checkout dev #切换到dev分支 git checkout -d dev ##创建并切换到dev git checkout -- readme.txt 回退更改内容在提交之前 [root@oldboy u02]# cat readme.txt 1 hehe 2 woshi 3 hehehheheh [root@oldboy u02]# cat readme.txt 1 hehe 2 woshi 3 hehehheheh 4 youare [root@oldboy u02]# git checkout -- readme.txt [root@oldboy u02]# cat readme.txt 1 hehe 2 woshi 3 hehehheheh
合并分支到master:
git checkout dev echo "this is dev" >dev.txt git add dev.txt git commit -m "add dev.txt" git checkout master git merge dev [root@oldboy u02]# git merge dev 更新 3eb47f8..ac81cbc Fast-forward dev.txt | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 dev.txt
删除分支:
git branch -d dev
查看分支:
git branch -a * master remotes/origin/master
冲突解决:
新建分支: git checkout -b te M dev.txt 切换到一个新分支 'te' echo "1.branch te" > readme.txt git add readme.txt git commit -m "add te" git checkout master echo "2.test" >> readme.txt git add readme.txt git commit -m "master change" 然后发生冲突: [root@oldboy u02]# git commit -m "master change" [master 99a04b3] master change 1 file changed, 1 insertion(+) [root@oldboy u02]# git merge te 自动合并 dev.txt 冲突(内容):合并冲突于 dev.txt 自动合并失败,修正冲突然后提交修正的结果。 [root@oldboy u02]# cat dev.txt <<<<<<< HEAD this is 111 ======= this is dev >>>>>>> te 发生冲突只能手动去改成一致的内容才能合并分支。 [root@oldboy u02]# cat dev.txt this is dev 再次合并分支 [root@oldboy u02]# git add dev.txt [root@oldboy u02]# git commit -m "test111" [master ff7ee0c] test111 [root@oldboy u02]# git merge te Already up-to-date.
把分支推送到远端
git push origin dev [root@oldboy u02]# git push origin te Counting objects: 13, done. Compressing objects: 100% (8/8), done. Writing objects: 100% (12/12), 984 bytes | 0 bytes/s, done. Total 12 (delta 3), reused 0 (delta 0) remote: Resolving deltas: 100% (3/3), done. To git@github.com:Getingjin/demo.git * [new branch] te -> te [root@oldboy u02]# git push origin dev Total 0 (delta 0), reused 0 (delta 0) To git@github.com:Getingjin/demo.git * [new branch] dev -> dev 切换分支发生冲突会导致无法切换对应分支,可使用-f强制切换 git checkout -f master 切换到分支 'master'
删除远程分支:
[root@oldboy u02]# git push origin --delete dev To git@github.com:Getingjin/demo.git - [deleted] dev
关于分支管理总结:
git branch -r #列出远端所有分支 git branch -a #列出所有分支包含远端和本地 git branch [branch-name] //创建新分支 git checkout –b [branch-name] //创建新分支,并且切换到该分支 git branch --track [branch] [remote-branch] //创建新分支,与指定的远程分支建立追踪关系 git checkout [branch-name] //切换至指定分支 git branch –set-upstream [branch] [remote-branch] //本地分支与远程分支建立追踪关系 git merge [branch] //合并指定分支到当前分支 git cherry-pick [commit] //选择一个commit,合并至当前分支 git branch –d [branch-name] //删除本地分支 git push origin --delete [branch-name] //删除远程分支 git branch –dr [remote/branch] //删除远程分支
git-标签管理
git tag v1.0 打标签 git tag 列出标签 git tag -d <tagname> 删除标签 git push origin v1.0 推送标签到远端 [root@oldboy u02]# git push origin v1.0 Counting objects: 22, done. Compressing objects: 100% (10/10), done. Writing objects: 100% (14/14), 1.25 KiB | 0 bytes/s, done. Total 14 (delta 5), reused 0 (delta 0) remote: Resolving deltas: 100% (5/5), completed with 1 local object. To git@github.com:Getingjin/demo.git * [new tag] v1.0 -> v1.0 git push origin --delete tag <tagname> //删除一个远程标签 [root@oldboy u02]# git push origin --delete tag v1.0 To git@github.com:Getingjin/demo.git - [deleted] v1.0 创建本地标签 创建带有说明的标签,-a指定标签名字,-m指定说明文字 git tag -a v1.0.0 -m "version 1.0.0 release"
标签管理小结:
命令git tag –a <tagname> -m "message" //在当前commit状态新建一个tag 命令git tag -l <tagname> //列出相对应版本号tag 命令git push origin <tagname> //推送一个本地标签 命令git push origin --tags //推送全部未推送过的本地标签 命令git tag -d <tagname> //删除一个本地标签 命令git push origin --delete tag <tagname> //删除一个远程标签 命令git push origin :refs/tags/<tagname> //删除一个远程标签
嗨、骚年、快来消灭0回复。