GIT04_分支概述查看创建切换合并分支代码冲突如何定位解决
Posted TZ845195485
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了GIT04_分支概述查看创建切换合并分支代码冲突如何定位解决相关的知识,希望对你有一定的参考价值。
文章目录
①. 分支概述
- ①. 几乎所有的版本控制系统都以某种形式支持分支。 使用分支意味着你可以把你的工作从开发主线上分离开来,以免影响开发主线。Git 的master分支并不是一个特殊分支。 它跟其它分支没有区别。 之所以几乎每一个仓库都有master 分支,是因为git init 命令默认创建它,并且大多数人都懒得去改动它。
- ②. 分支的操作
命令名称 | 作用 |
---|---|
git branch 分支名 | 创建分支 |
git branch -v(-r -a) | 查看分支 |
git checkout 分支名 | 切换分支 |
git merge 分支名 | 把指定的分支合并到当前分支上 |
②. 查看分支 git branch -a
-
①. 列出所有本地分支:git branch(git branch -v)
-
②. 列出所有远程分支:git branch -r
-
③. 列出所有本地分支和远程分支:git branch -a
③. 创建分支 git branch hot-fix
- git branch 分支名称
Administrator@SD-20191128LXMQ MINGW64 /d/gitRepos/repo1 (master)
$ git branch hot-fix
Administrator@SD-20191128LXMQ MINGW64 /d/gitRepos/repo1 (master)
$ git branch -v
hot-fix d39bee4 delete b.txt
* master d39bee4 delete b.txt
④. 切换分支 git checkout hot-fix
- git checkout 分支名称
Administrator@SD-20191128LXMQ MINGW64 /d/gitRepos/repo1 (master)
$ git checkout hot-fix
Switched to branch 'hot-fix'
M a.txt
Administrator@SD-20191128LXMQ MINGW64 /d/gitRepos/repo1 (hot-fix)
⑤. 合并分支 git merge hot-fix
-
①. git merge 分支名
-
②. 我们在 hot-fix的a.txt文件的最后追加了内容,提交了到本地库,现在切回到了master,在master上进行代码的合并操作
Administrator@SD-20191128LXMQ MINGW64 /d/gitRepos/repo1 (master)
$ cat a.txt
hello TANGZHI
hello TANGZHI
hello TANGZHI
hello TANGZHI
hello TANGZHI
hello TANGZHI
hello TANGZHI
hello TANGZHI
hello TANGZHI
第二次版本迭代
Administrator@SD-20191128LXMQ MINGW64 /d/gitRepos/repo1 (master)
$ git merge hot-fix
Updating d39bee4..ba6920a
Fast-forward
a.txt | 1 +
1 file changed, 1 insertion(+)
Administrator@SD-20191128LXMQ MINGW64 /d/gitRepos/repo1 (master)
$ cat a.txt
hello TANGZHI
hello TANGZHI
hello TANGZHI
hello TANGZHI
hello TANGZHI
hello TANGZHI
hello TANGZHI
hello TANGZHI
hello TANGZHI
第二次版本迭代
这是hot-fix分支修改了
- ③. 有时候合并操作不会如此顺利。 如果你在两个不同的分支中,对同一个文件的同一个部分进行了不同的修改,Git就没办法合并它们,同时会提示文件冲突。此时需要我们打开冲突的文件并修复冲突内容,最后执行git add命令来标识冲突已解决
(注意:如果我们要进行分支的合并,首先我们应该将master分支先合并到自己的分支,将冲突解决,然后再将自己的分支合并到master,这样就不会有问题)
- 这里有三个分支,master、hot-fix、TANGZHI
- 我们在hot-fix的a.txt文件中修改了内容,在最后一行添加了"这是hot-fix分支修改了"
- 我们在TANGZHI的a.txt文件中修改了内容,在最后一行添加了"TANGZHI分支进行了修改"
- 切回到主分支,将hot-fix分支合并到master这里不会产生问题,正常合并
- 切回到主分支,将TANGZHI分支合并到master产生了冲突,因为我们对同一个文件进行了修改
- 手动解决冲突,重新进行提交,注意提交的时候 git commit -m “这个后面不能带有文件名”
$ git branch TANGZHI
Administrator@SD-20191128LXMQ MINGW64 /d/gitRepos/repo1 (master)
$ git checkout TANGZHI
Switched to branch 'TANGZHI'
Administrator@SD-20191128LXMQ MINGW64 /d/gitRepos/repo1 (TANGZHI)
$ vim a.txt
Administrator@SD-20191128LXMQ MINGW64 /d/gitRepos/repo1 (TANGZHI)
$ git commit -am "TANGZHI分支追加了内容"
[TANGZHI fdd1068] TANGZHI分支追加了内容
1 file changed, 1 insertion(+)
Administrator@SD-20191128LXMQ MINGW64 /d/gitRepos/repo1 (TANGZHI)
$ git checkout master
Switched to branch 'master'
Administrator@SD-20191128LXMQ MINGW64 /d/gitRepos/repo1 (master)
$ git checkout hot-fix
Switched to branch 'hot-fix'
Administrator@SD-20191128LXMQ MINGW64 /d/gitRepos/repo1 (hot-fix)
$ vi a.txt
Administrator@SD-20191128LXMQ MINGW64 /d/gitRepos/repo1 (hot-fix)
$ git commit -am "hot-fix分支冲突问题"
[hot-fix eb0e4ea] hot-fix分支冲突问题
1 file changed, 1 insertion(+)
Administrator@SD-20191128LXMQ MINGW64 /d/gitRepos/repo1 (hot-fix)
$ git checkout master
Switched to branch 'master'
Administrator@SD-20191128LXMQ MINGW64 /d/gitRepos/repo1 (master)
$ git merge hot-fix
Updating ba6920a..eb0e4ea
Fast-forward
a.txt | 1 +
1 file changed, 1 insertion(+)
Administrator@SD-20191128LXMQ MINGW64 /d/gitRepos/repo1 (master)
$ cat a.txt
hello TANGZHI
hello TANGZHI
hello TANGZHI
hello TANGZHI
hello TANGZHI
hello TANGZHI
hello TANGZHI
hello TANGZHI
hello TANGZHI
第二次版本迭代
这是hot-fix分支修改了
hot-fix分支演示和TANGZHI分支的冲突问题
Administrator@SD-20191128LXMQ MINGW64 /d/gitRepos/repo1 (master)
$ git merge TANGZHI
Auto-merging a.txt
CONFLICT (content): Merge conflict in a.txt
Automatic merge failed; fix conflicts and then commit the result.
Administrator@SD-20191128LXMQ MINGW64 /d/gitRepos/repo1 (master|MERGING)
$ git merge TANGZHI
error: Merging is not possible because you have unmerged files.
hint: Fix them up in the work tree, and then use 'git add/rm <file>'
hint: as appropriate to mark resolution and make a commit.
fatal: Exiting because of an unresolved conflict.
Administrator@SD-20191128LXMQ MINGW64 /d/gitRepos/repo1 (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: a.txt
no changes added to commit (use "git add" and/or "git commit -a")
Administrator@SD-20191128LXMQ MINGW64 /d/gitRepos/repo1 (master|MERGING)
$ cat a.txt
hello TANGZHI
hello TANGZHI
hello TANGZHI
hello TANGZHI
hello TANGZHI
hello TANGZHI
hello TANGZHI
hello TANGZHI
hello TANGZHI
第二次版本迭代
这是hot-fix分支修改了
<<<<<<< HEAD
hot-fix分支演示和TANGZHI分支的冲突问题
=======
TANGZHI分支进行了修改
>>>>>>> TANGZHI
Administrator@SD-20191128LXMQ MINGW64 /d/gitRepos/repo1 (master|MERGING)
$ vi a.txt
Administrator@SD-20191128LXMQ MINGW64 /d/gitRepos/repo1 (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: a.txt
no changes added to commit (use "git add" and/or "git commit -a")
Administrator@SD-20191128LXMQ MINGW64 /d/gitRepos/repo1 (master|MERGING)
# 注意这里不能带文件名字,因为git不能识别是哪个分支下的文件
$ git commit -am "冲突解决了"
[master 0c17f90] 冲突解决了
Administrator@SD-20191128LXMQ MINGW64 /d/gitRepos/repo1 (master)
$ git stauts
git: 'stauts' is not a git command. See 'git --help'.
The most similar command is
status
Administrator@SD-20191128LXMQ MINGW64 /d/gitRepos/repo1 (master)
$ git status
On branch master
nothing to commit, working tree clean
Administrator@SD-20191128LXMQ MINGW64 /d/gitRepos/repo1 (master)
$ git checkout TANGZHI
Switched to branch 'TANGZHI'
Administrator@SD-20191128LXMQ MINGW64 /d/gitRepos/repo1 (TANGZHI)
$ cat a.txt
hello TANGZHI
hello TANGZHI
hello TANGZHI
hello TANGZHI
hello TANGZHI
hello TANGZHI
hello TANGZHI
hello TANGZHI
hello TANGZHI
第二次版本迭代
这是hot-fix分支修改了
TANGZHI分支进行了修改
以上是关于GIT04_分支概述查看创建切换合并分支代码冲突如何定位解决的主要内容,如果未能解决你的问题,请参考以下文章
Git 第七章 IDEA集成Git -- IDEA集成Git( 创建分支 & 重命名分支 & 切换分支 & 合并分支(不冲突 / (代码)冲突) )