4. Git分支操作
4.1 什么是分支
-
在版本控制过程中,同时推进多个任务,为每个任务,我们就可以创建每个任务的单独分支。使用分支意味着程序员可以把自己的工作从开发主线上分离开来, 开发自己分支的时候,不会影响主线分支的运行。对于初学者而言,分支可以简单理解为副本,一个分支就是一个单独的副本。(分支底层其实也是指针的引用)
-
图解:
4.2 分支的好处
- 同时并行推进多个功能开发,提高开发效率
- 各个分支在开发过程中,如果某一个分支开发失败,不会对其他分支有任何影响。失败的分支删除重新开始即可
4.3 分支的操作
命令名称 | 作用 |
---|---|
git branch 分支名 | 创建分支 |
git branch -v | 查看分支 |
git checkout 分支名 | 切换分支 |
git merge 分支名 | 把指定的分支合并到当前分支上 |
-
查看分支
-
基本语法:
git branch -v
-
实例操作:
lenovo@DESKTOP-55E400K MINGW64 /e/Git-space/project_1 (master) $ git branch -v * master f5dc083 first commit
-
-
创建分支
-
基本语法:
git branch 分支名
-
实例操作:
lenovo@DESKTOP-55E400K MINGW64 /e/Git-space/project_1 (master) $ git branch hot-fix lenovo@DESKTOP-55E400K MINGW64 /e/Git-space/project_1 (master) $ git branch -v hot-fix f5dc083 first commit //新分支 * master f5dc083 first commit
-
-
切换分支
-
基本语法:
git checkout 分支名
-
实例操作:
lenovo@DESKTOP-55E400K MINGW64 /e/Git-space/project_1 (master) $ git checkout hot-fix Switched to branch 'hot-fix' lenovo@DESKTOP-55E400K MINGW64 /e/Git-space/project_1 (hot-fix)
-
图解:
-
master、 hot-fix 其实都是指向具体版本记录的指针,所以创建分支的本质就是多创建一个指针
-
当前所在的分支,其实是由HEAD决定的
- HEAD 如果指向 master,那么我们现在就在 master 分支上
- HEAD 如果执行 hotfix,那么我们现在就在 hotfix 分支上
- 所以切换分支的本质就是移动 HEAD 指针
-
-
-
修改 hot-fix 分支
//在hot-fix分支上做修改,并添加到暂存区,并提交到本地库 lenovo@DESKTOP-55E400K MINGW64 /e/Git-space/project_1 (hot-fix) $ vim hello.txt lenovo@DESKTOP-55E400K MINGW64 /e/Git-space/project_1 (hot-fix) $ git add hello.txt lenovo@DESKTOP-55E400K MINGW64 /e/Git-space/project_1 (hot-fix) $ git commit -m "hot-fix first commit" hello.txt [hot-fix 8753437] hot-fix first commit 1 file changed, 2 insertions(+), 2 deletions(-) lenovo@DESKTOP-55E400K MINGW64 /e/Git-space/project_1 (hot-fix) $ git status On branch hot-fix nothing to commit, working tree clean lenovo@DESKTOP-55E400K MINGW64 /e/Git-space/project_1 (hot-fix) $ cat hello.txt hello git 1111111 hello git 2222222 hello git hello git hello git hello git hello git hello git hello git hello git hello git
-
查看当前日志
lenovo@DESKTOP-55E400K MINGW64 /e/Git-space/project_1 (hot-fix) $ git reflog //当前指针指向了hot-fix 8753437 (HEAD -> hot-fix) HEAD@{0}: commit: hot-fix first commit f5dc083 (master) HEAD@{1}: checkout: moving from master to hot-fix f5dc083 (master) HEAD@{2}: reset: moving to f5dc083 8c7d395 HEAD@{3}: reset: moving to HEAD 8c7d395 HEAD@{4}: commit: first commit f5dc083 (master) HEAD@{5}: commit (initial): first commit
-
回到master分支,查看文件
lenovo@DESKTOP-55E400K MINGW64 /e/Git-space/project_1 (hot-fix) $ git checkout master Switched to branch 'master' lenovo@DESKTOP-55E400K MINGW64 /e/Git-space/project_1 (master) $ cat hello.txt hello git //未合并前,文件内容仍是之前的 hello git hello git hello git hello git hello git hello git hello git hello git hello git hello git
-
合并分支
-
基本语法:
git merge 分支名
-
实例操作:
lenovo@DESKTOP-55E400K MINGW64 /e/Git-space/project_1 (master) $ git merge hot-fix Updating f5dc083..8753437 Fast-forward hello.txt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) lenovo@DESKTOP-55E400K MINGW64 /e/Git-space/project_1 (master) $ cat hello.txt hello git 1111111 // master分支上的文件内容已经修改 hello git 2222222 hello git hello git hello git hello git hello git hello git hello git hello git hello git
-
4.4 分支冲突问题
-
冲突产生的原因:合并分支时,两个分支在同一个文件的同一个位置有两套完全不同的修改。 Git 无法替我们决定使用哪一个。必须人为决定新代码内容
-
在master分支上修改文件, 添加到暂存区,并提交到本地库
lenovo@DESKTOP-55E400K MINGW64 /e/Git-space/project_1 (master) $ vim hello.txt lenovo@DESKTOP-55E400K MINGW64 /e/Git-space/project_1 (master) $ git status On branch master Changes not staged for commit: (use "git add <file>..." to update what will be committed) (use "git restore <file>..." to discard changes in working directory) modified: hello.txt no changes added to commit (use "git add" and/or "git commit -a") lenovo@DESKTOP-55E400K MINGW64 /e/Git-space/project_1 (master) $ git add hello.txt lenovo@DESKTOP-55E400K MINGW64 /e/Git-space/project_1 (master) $ git commit -m "master test" hello.txt [master f8b3939] master test 1 file changed, 1 insertion(+), 1 deletion(-) lenovo@DESKTOP-55E400K MINGW64 /e/Git-space/project_1 (master) $ git status On branch master nothing to commit, working tree clean
-
切换到hot-fix 分支上,对文件修改
lenovo@DESKTOP-55E400K MINGW64 /e/Git-space/project_1 (master) $ git checkout hot-fix Switched to branch 'hot-fix' lenovo@DESKTOP-55E400K MINGW64 /e/Git-space/project_1 (hot-fix) $ vim hello.txt lenovo@DESKTOP-55E400K MINGW64 /e/Git-space/project_1 (hot-fix) $ vim hello.txt lenovo@DESKTOP-55E400K MINGW64 /e/Git-space/project_1 (hot-fix) $ git add hello.txt lenovo@DESKTOP-55E400K MINGW64 /e/Git-space/project_1 (hot-fix) $ git commit -m "hot-fix test" hello.txt [hot-fix 184ab7a] hot-fix test 1 file changed, 2 insertions(+), 1 deletion(-)
-
回到master分支进行合并
lenovo@DESKTOP-55E400K MINGW64 /e/Git-space/project_1 (hot-fix) $ git checkout master Switched to branch 'master' lenovo@DESKTOP-55E400K MINGW64 /e/Git-space/project_1 (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. //Automatic merge failed自动合并失败 //两个分支在同一个文件的同一个位置有两套完全不同的修改,不知道选哪个
-
手动合并:
-
此时文件内容
hello git 1111111 hello git 2222222 hello git hello git hello git hello git hello git hello git hello git <<<<<<< HEAD hello git master hello git ======= hello git hello git hot-fix >>>>>>> hot-fix
-
手动合并
hello git 1111111 hello git 2222222 hello git hello git hello git hello git hello git hello git hello git hello git master hello git hot-fix
-
-
查看此时的状态
lenovo@DESKTOP-55E400K MINGW64 /e/Git-space/project_1 (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 <file>..." to mark resolution) both modified: hello.txt //有文件未被检测 no changes added to commit (use "git add" and/or "git commit -a")
-
将手动合并的文件添加、提交
lenovo@DESKTOP-55E400K MINGW64 /e/Git-space/project_1 (master|MERGING) $ git add hello.txt lenovo@DESKTOP-55E400K MINGW64 /e/Git-space/project_1 (master|MERGING) $ git commit -m "merge test" hello.txt fatal: cannot do a partial commit during a merge. lenovo@DESKTOP-55E400K MINGW64 /e/Git-space/project_1 (master|MERGING) $ git commit -m "merge test" [master 694ba18] merge test lenovo@DESKTOP-55E400K MINGW64 /e/Git-space/project_1 (master) $ cat hello.txt hello git 1111111 hello git 2222222 hello git hello git hello git hello git hello git hello git hello git hello git master //此时文件已经解决冲突,完成合并 hello git hot-fix