Git应用进阶(二)-创新互联
Git进阶(二)
创新互联公司是由多位在大型网络公司、广告设计公司的优秀设计人员和策划人员组成的一个具有丰富经验的团队,其中包括网站策划、网页美工、网站程序员、网页设计师、平面广告设计师、网络营销人员及形象策划。承接:成都做网站、成都网站建设、成都外贸网站建设、网站改版、网页设计制作、网站建设与维护、网络推广、数据库开发,以高性价比制作企业网站、行业门户平台等全方位的服务。===============================================================================
概述:
===============================================================================
Git 分支
1.分支命名法则及常用命令
master(主干分支) 是指向指定分支的最近一次提交;
dev 是指向dev分支的最近一次提交;
不管有多少分支,活动分支只能有一个;
head 一定是指向某个分支的,head表示 映射了当前工作目录当中所反映的最近一次提交;即 head始终指向活动分支的最近一次提交;
★分支命名法则
可以使用"/",但不能使用"/"结尾;
不能以 "-" 开头;
以位于 "/" 后面的组件,不能以 "." 开头;
不能使用连续的 "...";
不能使用空白字符" ";
不能使用"^","~","?","*","[" 等符号
必须唯一,分支的名字始终指向目标分支的最近一次提交;
★git branch :列出,创建及删除分支
git branch BRANCH_NAME [START_COMMIT]
git branch -d BRANCH_NAME 删除分支
★git show-branch:查看分支及其相关的提交
★git checkout
git checkout
检出分支
演示1:git 创建分支
[root@node1 test]# ls first.sh INSTALL readmin subdir [root@node1 test]# git branch --list * master # 带"*"表示当前分支 [root@node1 test]# git log commit 3c0b6864718ec8f8aebb5b66fbfd65b757504169 (HEAD -> master) Author: watao演示2:git show-branch 查看分支
# git show-branch 查看具体的分支信息 [root@node1 test]# git show-branch ! [bug/first] v1.1 ! [dev] v1.0 * [master] v1.1 --- + * [bug/first] v1.1 ++* [dev] v1.0 [root@node1 test]# git show-branch dev [dev] v1.0 [root@node1 test]# [root@node1 test]# git show-branch bug/first [bug/first] v1.1演示3:git checkout BRANCH_NAME 切换分支
[root@node1 taotao]# git checkout dev #切换分支 Switched to branch 'dev' [root@node1 taotao]# [root@node1 taotao]# git branch --list bug/first * dev master [root@node1 taotao]# [root@node1 taotao]# git show-branch ! [bug/first] v1.1 * [dev] v1.0 ! [master] v1.1 --- + + [bug/first] v1.1 +*+ [dev] v1.0 [root@node1 taotao]# [root@node1 taotao]# [root@node1 taotao]# git log commit 5d4298d6fdcbb6276e69f002e7148210124e52d9 (HEAD -> dev) Author: watao演示4:git checkout -d BRANCH_NAME 删除分支
[root@node1 taotao]# git branch -d bug/first Deleted branch bug/first (was 5685269). [root@node1 taotao]# git branch --list dev * masterGit 分支合并
1.分支合并--git merge
★相关概念
合并基础:要合并的分支的最近一次的共同提交;
我们的版本:当前分支的最近一次提交;
他们的版本:要合并进来的分支的最近一次提交;
☉无冲突合并:
git checkout master; 检出到主分支
git status 检查是否有未提交的信息
git merge BRANCH_NAME 从他们的版本合并到我们的版本
git log --graph --pretty=oneline --abbrev-commit 以图形的方式查看
☉有冲突合并:
手动解决冲突;
解决完成之后:git add ---> git commit
☉回退到合并之前的版本
git reset --hard ORIG_HEAD
演示1:无冲突的合并
[root@node1 taotao]# git branch --list dev * master [root@node1 taotao]# [root@node1 taotao]# [root@node1 taotao]# git merge dev Merge made by the 'recursive' strategy. first.sh | 1 + second.sh | 2 ++ 2 files changed, 3 insertions(+) create mode 100644 second.sh [root@node1 taotao]# [root@node1 taotao]# git status On branch master nothing to commit, working tree clean [root@node1 taotao]# git show-branch ! [dev] v1.1.1-dev * [master] Merge branch 'dev' -- - [master] Merge branch 'dev' +* [dev] v1.1.1-dev [root@node1 taotao]# git log --graph --pretty=oneline --abbrev-commit * 43d8e9a (HEAD -> master) Merge branch 'dev' |\ | * 21a0411 (dev) v1.1.1-dev | * 587719d v1.1-dev * | 5685269 v1.1 |/ * 5d4298d v1.0 * b918244 v0.0.2 * 1b5d8e0 v0.0.1演示2:有冲突的合并
[root@node1 taotao]# echo "taotaohuihui" > my.txt [root@node1 taotao]# git add my.txt [root@node1 taotao]# git commit -m "v2.0" [master 1df3da7] v2.0 1 file changed, 1 insertion(+) create mode 100644 my.txt [root@node1 taotao]# git log --graph --pretty=oneline --abbrev-commit * 1df3da7 (HEAD -> master) v2.0 # 新创建的提交 * 43d8e9a Merge branch 'dev' |\ | * 21a0411 (dev) v1.1.1-dev | * 587719d v1.1-dev * | 5685269 v1.1 |/ * 5d4298d v1.0 * b918244 v0.0.2 * 1b5d8e0 v0.0.1 [root@node1 taotao]# git branch dev fatal: A branch named 'dev' already exists. #dev分支存在不能创建相同的,实际上可以删除了,因为已经合并到master分支上去了 [root@node1 taotao]# [root@node1 taotao]# [root@node1 taotao]# git branch fotfix # 在master 2.0 的基础上常见fotfix分支 [root@node1 taotao]# git show-branch ! [dev] v1.1.1-dev ! [fotfix] v2.0 * [master] v2.0 --- +* [fotfix] v2.0 -- [fotfix^] Merge branch 'dev' ++* [dev] v1.1.1-dev [root@node1 taotao]# echo "third line" >> first.sh # 修改master分支的内容 [root@node1 taotao]# cat first.sh #!/bin/bash echo "hello world" echo "new date" third line [root@node1 taotao]# git add first.sh [root@node1 taotao]# git commit -m "v2.1" [master 697a6c5] v2.1 1 file changed, 1 insertion(+) [root@node1 taotao]# git show-branch ! [dev] v1.1.1-dev ! [fotfix] v2.0 * [master] v2.1 --- * [master] v2.1 +* [fotfix] v2.0 -- [fotfix^] Merge branch 'dev' ++* [dev] v1.1.1-dev [root@node1 taotao]# git checkout fotfix #切换到fotfix分支 Switched to branch 'fotfix' [root@node1 taotao]# ls first.sh INSTALL my.txt readmin second.sh subdir [root@node1 taotao]# [root@node1 taotao]# echo "new line" >> first.sh # 编辑first文件并提交 [root@node1 taotao]# cat first.sh #!/bin/bash echo "hello world" echo "new date" new line [root@node1 taotao]# git add first.sh [root@node1 taotao]# git commit -m "v2.0-1" [fotfix 26b42ef] v2.0-1 1 file changed, 1 insertion(+) [root@node1 taotao]# git show-branch ! [dev] v1.1.1-dev * [fotfix] v2.0-1 ! [master] v2.1 --- * [fotfix] v2.0-1 + [master] v2.1 *+ [fotfix^] v2.0 -- [fotfix~2] Merge branch 'dev' +*+ [dev] v1.1.1-dev # 切回主分支,然后合并 fotfix分支,发现报错,即内容发生冲突。需手动修复冲突后再合并 [root@node1 taotao]# git checkout master Switched to branch 'master' [root@node1 taotao]# [root@node1 taotao]# [root@node1 taotao]# git merge fotfix Auto-merging first.sh CONFLICT (content): Merge conflict in first.sh Automatic merge failed; fix conflicts and then commit the result. 不能合并成功的文件,发现三个版本中 first.sh 内容各不相同 [root@node1 taotao]# git ls-files INSTALL first.sh first.sh first.sh my.txt readmin second.sh subdir/1.txt [root@node1 taotao]# [root@node1 taotao]# git ls-files --unmerged 100644 816a1b1e686cf6adb716b10e1a9eed870b2f18fa 1first.sh # "1" 表示合并基础 100644 3adec3b3bf2616c52eaba36bd8af2e79e9ef6495 2first.sh # "2" 表示我们的版本 100644 e45ccd5a231f9debdd84ffd621cdc3acfc8d15f1 3first.sh # "3" 表示他们的版本 [root@node1 taotao]# cat first.sh #!/bin/bash echo "hello world" echo "new date" <<<<<<< HEAD third line ======= new line >>>>>>> fotfix [root@node1 taotao]# git diff diff --cc first.sh index 3adec3b,e45ccd5..0000000 --- a/first.sh +++ b/first.sh @@@ -1,4 -1,4 +1,8 @@@ #!/bin/bash echo "hello world" echo "new date" ++<<<<<<< HEAD "<<<<"或">>>>>"为三方合并标记,由2个 ++ 开头 +third line #为主分支最近一次提交的内容 ++======= + new line ++>>>>>>> fotfix #为其他版本最近一次提交的内容 #直接在冲突文件的基础上修改即可,如想要都保留的话就把"三方合并标记"给删除 [root@node1 taotao]# vim first.sh [root@node1 taotao]# git diff diff --cc first.sh index 3adec3b,e45ccd5..0000000 --- a/first.sh +++ b/first.sh @@@ -1,4 -1,4 +1,5 @@@ #!/bin/bash echo "hello world" echo "new date" +third line #新加的两行 + new line [root@node1 taotao]# cat first.sh #!/bin/bash echo "hello world" echo "new date" third line new line #修改好之后重新添加到索引并提交 [root@node1 taotao]# git add first.sh [root@node1 taotao]# git commit #出现内容提示,保存即可 [master dd73764] Merge branch 'fotfix' [root@node1 taotao]# git log --graph --pretty=oneline --abbrev-commit * dd73764 (HEAD -> master) Merge branch 'fotfix' |\ | * 26b42ef (fotfix) v2.0-1 * | 697a6c5 v2.1 |/ * 1df3da7 v2.0 * 43d8e9a Merge branch 'dev' |\ | * 21a0411 (dev) v1.1.1-dev | * 587719d v1.1-dev * | 5685269 v1.1 |/ * 5d4298d v1.0 * b918244 v0.0.2 * 1b5d8e0 v0.0.1演示3:回到合并之前的版本
#回到合并之前的版本 [root@node1 taotao]# git reset --hard ORIG_HEAD HEAD is now at 697a6c5 v2.1 [root@node1 taotao]# git log --graph --pretty=oneline --abbrev-commit * 697a6c5 (HEAD -> master) v2.1 * 1df3da7 v2.0 * 43d8e9a Merge branch 'dev' |\ | * 21a0411 (dev) v1.1.1-dev | * 587719d v1.1-dev * | 5685269 v1.1 |/ * 5d4298d v1.0 * b918244 v0.0.2 * 1b5d8e0 v0.0.1 [root@node1 taotao]# git show-branch ! [dev] v1.1.1-dev ! [fotfix] v2.0-1 * [master] v2.1 --- + [fotfix] v2.0-1 * [master] v2.1 +* [fotfix^] v2.0 -- [fotfix~2] Merge branch 'dev' ++* [dev] v1.1.1-dev [root@node1 taotao]# cat first.sh #!/bin/bash echo "hello world" echo "new date" third line #原来的文件还在 [root@node1 taotao]# git checkout fotfix Switched to branch 'fotfix' [root@node1 taotao]# cat first.sh #!/bin/bash echo "hello world" echo "new date" new line #原来的文件还在1.git变基分支合并--git rebase
★变基操作
$ git checkout dev : 首先切换到要变基的分支;
$ git rebase master: 把他的基从最近一次的共同祖先提交转换为master的最新提交;
$ git checkout master :回到master分支;
$ git merge -m "MSG" :master 获取新的提交。
演示:
[root@node1 taotao]# git checkout master Switched to branch 'master' [root@node1 taotao]# ls first.sh INSTALL my.txt readmin second.sh subdir [root@node1 taotao]# cat first.sh #!/bin/bash echo "hello world" echo "new date" third line [root@node1 taotao]# vim first.sh [root@node1 taotao]# cat first.sh #编辑文件先解决冲突,以便于演示git变基操作 #!/bin/bash echo "hello world" echo "new date" #再次提交一次 [root@node1 taotao]# git add first.sh [root@node1 taotao]# git commit -m "v2.2" [master 7961be2] v2.2 1 file changed, 1 deletion(-) [root@node1 taotao]# git log --graph --pretty=oneline --abbrev-commit * 7961be2 (HEAD -> master) v2.2 * 697a6c5 v2.1 * 1df3da7 v2.0 * 43d8e9a Merge branch 'dev' |\ | * 21a0411 (dev) v1.1.1-dev | * 587719d v1.1-dev * | 5685269 v1.1 |/ * 5d4298d v1.0 * b918244 v0.0.2 * 1b5d8e0 v0.0.1 #切换到要变基的分支 [root@node1 taotao]# git checkout fotfix Switched to branch 'fotfix' [root@node1 taotao]# ls first.sh INSTALL my.txt readmin second.sh subdir [root@node1 taotao]# cat first.sh #!/bin/bash echo "hello world" echo "new date" new line # 新增加的一行 #执行变基操作,表示把当前 fotfix的基变为master分支的最近一次提交 [root@node1 taotao]# git rebase master First, rewinding head to replay your work on top of it... Applying: v2.0-1 [root@node1 taotao]# cat first.sh #!/bin/bash echo "hello world" echo "new date" new line [root@node1 taotao]# git checkout master Switched to branch 'master' [root@node1 taotao]# cat first.sh #!/bin/bash echo "hello world" echo "new date" # 此时没有新行,需执行合并操作 [root@node1 taotao]# git merge fotfix Updating 7961be2..3eaaead Fast-forward # 快进合并 first.sh | 1 + 1 file changed, 1 insertion(+) [root@node1 taotao]# cat first.sh #!/bin/bash echo "hello world" echo "new date" new line [root@node1 taotao]# git log --graph --pretty=oneline --abbrev-commit * 3eaaead (HEAD -> master, fotfix) v2.0-1 * 7961be2 v2.2 * 697a6c5 v2.1 * 1df3da7 v2.0 * 43d8e9a Merge branch 'dev' |\ | * 21a0411 (dev) v1.1.1-dev | * 587719d v1.1-dev * | 5685269 v1.1 |/ * 5d4298d v1.0 * b918244 v0.0.2 * 1b5d8e0 v0.0.1另外有需要云服务器可以了解下创新互联cdcxhl.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。
分享标题:Git应用进阶(二)-创新互联
链接分享:http://pwwzsj.com/article/dpodog.html