GIT04_分支概述、查看、创建、切换、合并分支、代码冲突如何定位解决(下)

⑤. 合并分支 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分支进行了修改


上一篇:查找某文件夹下所有子文件夹内的py文件-3 一行代码搞定


下一篇:人生九度