开发人员提交代码,生产环境部署在dev分支
线上环境部署在master分支上。
什么是分支在版本控制过程中,同时推进多个任务,为每个任务,我们就可以创建每个任务的单独 分支。使用分支意味着程序员可以把自己的工作从开发主线上分离开来, 开发自己分支的时 候,不会影响主线分支的运行。对于初学者而言,分支可以简单理解为副本,一个分支就是 一个单独的副本。(分支底层其实也是指针的引用 )
同时并行推进多个功能开发,提高开发效率。 各个分支在开发过程中,如果某一个分支开发失败,不会对其他分支有任何影响。失败 的分支删除重新开始即可。
分支的操作 命令名称作用git branch 分支名创建分支git branch -v查看分支git checkout 分支名切换分支git merge 分支名把指定的分支合并到当前分支上 查看分支 1) 基本语法git branch -v
2)案例实操
Administrator@WIN-NNHMK0M7785 MINGW64 /e/test (master)
$ git branch -v
* master 611d18f second commit hello.txt
创建分支
1) 基本语法
git branch 分支名
2)案例实操
Administrator@WIN-NNHMK0M7785 MINGW64 /e/test (master)
$ git branch hot-fix
Administrator@WIN-NNHMK0M7785 MINGW64 /e/test (master)
$ git branch -v
hot-fix 611d18f second commit hello.txt
* master 611d18f second commit hello.txt
hot-fix就是刚创建的新的分支,并将主分支 master的内容复制了一份
修改分支在 maste 分支上做修改
Administrator@WIN-NNHMK0M7785 MINGW64 /e/test (master)
$ vim hello.txt
hello git123456789
hello git111111111
hello git
hello git
hello git
hello git
hello git
hello git
:wq退出并保存
提交本地库
Administrator@WIN-NNHMK0M7785 MINGW64 /e/test (master)
$ git status
On branch master
Changes not staged for commit:
(use "git add ..." to update what will be committed)
(use "git restore ..." to discard changes in working directory)
modified: hello.txt
no changes added to commit (use "git add" and/or "git commit -a")
Administrator@WIN-NNHMK0M7785 MINGW64 /e/test (master)
$ git add hello.txt
Administrator@WIN-NNHMK0M7785 MINGW64 /e/test (master)
$ git commit -m "third commit hello.txt"
[master 792eed1] third commit hello.txt
1 file changed, 1 insertion(+), 1 deletion(-)
查看分支
Administrator@WIN-NNHMK0M7785 MINGW64 /e/test (master)
$ git branch -v
hot-fix 611d18f second commit hello.txt
* master 792eed1 third commit hello.txt
hot-fix 分支并未做任何改变
当前 master 分支已更新为最新一次提交的版本
查看 master 分支上的文件内容
Administrator@WIN-NNHMK0M7785 MINGW64 /e/test (master)
$ cat hello.txt
hello git123456789
hello git111111111
hello git
hello git
hello git
hello git
hello git
hello git
切换分支
1) 基本语法
git checkout 分支名
2)案例实操
Administrator@WIN-NNHMK0M7785 MINGW64 /e/test (master)
$ git checkout hot-fix
Switched to branch 'hot-fix'
发现当先分支已由 master 改为 hot-fix
查看 hot-fix 分支上的文件内容发现与 master 分支上的内容不同
Administrator@WIN-NNHMK0M7785 MINGW64 /e/test (hot-fix)
$ cat hello.txt
hello git123456789
hello git
hello git
hello git
hello git
hello git
hello git
hello git
在 hot-fix 分支上做修改
Administrator@WIN-NNHMK0M7785 MINGW64 /e/test (hot-fix)
$ vim hello.txt
修改
hello git123456789
hello git123456789
hello git
hello git
hello git
hello git
hello git
hello git
:wq退出并保存
提交本地库
Administrator@WIN-NNHMK0M7785 MINGW64 /e/test (hot-fix)
$ git status
On branch hot-fix
Changes not staged for commit:
(use "git add ..." to update what will be committed)
(use "git restore ..." to discard changes in working directory)
modified: hello.txt
no changes added to commit (use "git add" and/or "git commit -a")
Administrator@WIN-NNHMK0M7785 MINGW64 /e/test (hot-fix)
$ git add hello.txt
Administrator@WIN-NNHMK0M7785 MINGW64 /e/test (hot-fix)
$ git commit -m "hot-fix first commit" hello.txt
[hot-fix 6a30697] hot-fix first commit
1 file changed, 1 insertion(+), 1 deletion(-)
合并分支
1) 基本语法
git merge 分支名
2)案例实操 在 master 分支上合并 hot-fix 分支
首先切换到master分支
Administrator@WIN-NNHMK0M7785 MINGW64 /e/test (hot-fix)
$ git checkout master
Switched to branch 'master'
合并
Administrator@WIN-NNHMK0M7785 MINGW64 /e/test (master)
$ git merge hot-fix
Auto-merging hello.txt
CONFLICT (content): Merge conflict in hello.txt
Automatic merge failed; fix conflicts and then commit the result.
合并分支产生冲突
产生冲突冲突产生的表现: 后面状态为 MERGING
Administrator@WIN-NNHMK0M7785 MINGW64 /e/test (master|MERGING)
$ cat hello.txt
hello git123456789
hot-fix
hello git
hello git
hello git
hello git
hello git
hello git
冲突产生的原因: 合并分支时,两个分支在同一个文件的同一个位置有两套完全不同的修改。 Git 无法替 我们决定使用哪一个。必须人为决定新代码内容。
查看状态(检测到有文件有两处修改)
Administrator@WIN-NNHMK0M7785 MINGW64 /e/test (master|MERGING)
$ git status
On branch master
You have unmerged paths.
(fix conflicts and run "git commit")
(use "git merge --abort" to abort the merge)
Unmerged paths:
(use "git add ..." to mark resolution)
both modified: hello.txt
no changes added to commit (use "git add" and/or "git commit -a")
解决冲突
1) 编辑有冲突的文件,删除特殊符号,决定要使用的内容
特殊符号: > hot-fix
hello git123456789
hot-fix
hello git
hello git
hello git
hello git
hello git
hello git
修改后 保留master的修改
Administrator@WIN-NNHMK0M7785 MINGW64 /e/test (master|MERGING)
$ vim hello.txt
hello git123456789
hello git111111111
hello git
hello git
hello git
hello git
hello git
hello git
:wq保存
2) 添加到暂存区Administrator@WIN-NNHMK0M7785 MINGW64 /e/test (master|MERGING)
$ git add hello.txt
3) 执行提交(注意: 此时使用 git commit 命令时不能带文件名)
Administrator@WIN-NNHMK0M7785 MINGW64 /e/test (master|MERGING)
$ git commit -m "merge need master hello.txt"
[master 23aa6ad] merge need master hello.txt
发现后面 MERGING 消失, 变为正常
Administrator@WIN-NNHMK0M7785 MINGW64 /e/test (master)
$ git status
On branch master
nothing to commit, working tree clean
创建分支和切换分支图解
master、 hot-fix 其实都是指向具体版本记录的指针。当前所在的分支,其实是由 HEAD 决定的。所以创建分支的本质就是多创建一个指针。 HEAD 如果指向 master,那么我们现在就在 master 分支上。 HEAD 如果执行 hotfix,那么我们现在就在 hotfix 分支上 。
所以切换分支的本质就是移动 HEAD 指针。