跳转至

11. git分支冲突

11. git分支冲突

1.创建并切换到分支

#1.创建并切换到分支
git checkout -b cp_branch3

#2.查看分支列表以及当前所在的分支
git branch

#3.查看分支的版本
git log --oneline
[root@chupeng git04]# git checkout -b cp_branch3
Switched to a new branch 'cp_branch3'
[root@chupeng git04]# git branch
* cp_branch3
  master
[root@chupeng git04]# git log --oneline
a0c040a cp2分支第一次提交
e4c9c8a 第二次提交
db2fda5 首次提交
[root@chupeng git04]# ls
branch2.txt  chupeng.txt  cp_v2

2.在分支下创建文件并提交

#1.在分支3中模拟创建代码
echo "hebing" >hebing.txt

#2.提交
git add .
git commit -am "分支3第一次提交"

#4. 查看版本日志
git log --oneline
[root@chupeng git04]# echo "branch hebing neirong " > hebing.txt
[root@chupeng git04]# git add .
[root@chupeng git04]# git commit -m "cp3分支第一次提交"
[cp_branch3 7d96926] cp3分支第一次提交
 1 file changed, 1 insertion(+)
 create mode 100644 hebing.txt
[root@chupeng git04]# git log --oneline
7d96926 cp3分支第一次提交
a0c040a cp2分支第一次提交
e4c9c8a 第二次提交
db2fda5 首次提交
[root@chupeng git04]# ls
branch2.txt  chupeng.txt  cp_v2  hebing.txt

3.在master分支下也创建一个同名文件,提交

#1.切换到master分支
git checkout master

#2. 确认当前所在的分支
git branch

#3. 查看master分支的本地文件
ls

#4. 创建冲突文件
echo "master hebing" >hebing.txt

#5. 提交
git add .
git commit -am "master分支创建hebing.txt"

#6. 查看跟踪状态
git status

#7. 查看版本日志
git log --oneline
[root@chupeng git04]# git checkout master
Switched to branch 'master'
[root@chupeng git04]# git branch
  cp_branch3
* master
[root@chupeng git04]# ls
branch2.txt  chupeng.txt  cp_v2
[root@chupeng git04]# echo "master hebing neirong " > hebing.txt
[root@chupeng git04]# git add .
[root@chupeng git04]# git commit -m "这是master提交的合并测试版本"
[master 3cf29ce] 这是master提交的合并测试版本
 1 file changed, 1 insertion(+)
 create mode 100644 hebing.txt
[root@chupeng git04]# git status
# On branch master
nothing to commit, working directory clean
[root@chupeng git04]# git log --oneline
3cf29ce 这是master提交的合并测试版本
a0c040a cp2分支第一次提交
e4c9c8a 第二次提交
db2fda5 首次提交
[root@chupeng git04]# ls
branch2.txt  chupeng.txt  cp_v2  hebing.txt

4.在master分支上,合并cp_branch3分支

# 由于master分支上和分支3上都有hebing.txt文件,合并测试
#1. 查看当前所在的分支,确保在master分支
git branch

#2. master分支合并分支3,(发现出现冲突提醒)
git merge cp_branch3
[root@chupeng git04]# git branch
  cp_branch3
* master
[root@chupeng git04]# git merge cp_branch3
Auto-merging hebing.txt
CONFLICT (add/add): Merge conflict in hebing.txt
Automatic merge failed; fix conflicts and then commit the result.

5.查看同名文件

#1.查看冲突文件内容,
# 发现 ======= 上面的是master创建的内容,下面是分支创建的内容
cat hebing.txt

#2.查看跟踪状态,提示冲突文件需要解决
git status
[root@chupeng git04]# ls
branch2.txt  chupeng.txt  cp_v2  hebing.txt
[root@chupeng git04]# cat hebing.txt
<<<<<<< HEAD
master hebing neirong
=======
branch hebing neirong
>>>>>>> cp_branch3
[root@chupeng git04]# git status
# On branch master
# You have unmerged paths.
#   (fix conflicts and run "git commit")
#
# Unmerged paths:
#   (use "git add <file>..." to mark resolution)
#
#       both added:         hebing.txt
#
no changes added to commit (use "git add" and/or "git commit -a")

6.手动修改同名文件内容,保留需要的

# 手动修改冲突文件,可以只保留master,也可以只保留branch的,也可以都保留!
vim hebing.txt
[root@chupeng git04]# vim hebing.txt
[root@chupeng git04]# cat hebing.txt
master hebing neirong
branch hebing neirong

7.提交修改完的版本

# 解决完冲突后,就可以提交代码了!
#1.提交代码
git add .
git commit -am "合并完成版本!"

#2.查看跟踪状态
git status

#3.查看版本日志
git log --oneline
[root@chupeng git04]# git add .
[root@chupeng git04]# git commit -m "合并完成版本"
[master 5eb8f39] 合并完成版本
[root@chupeng git04]# git status
# On branch master
nothing to commit, working directory clean
[root@chupeng git04]# git log --oneline
5eb8f39 合并完成版本
3cf29ce 这是master提交的合并测试版本
7d96926 cp3分支第一次提交
a0c040a cp2分支第一次提交
e4c9c8a 第二次提交
db2fda5 首次提交

最后更新: 2022-02-18 11:26:47