协同开发时代码冲突的解决

git系列文章目录

第六章 协同开发时代码冲突的解决


前言

团队协同开发时难免遇到代码冲突,如何解决冲突和避免冲突。

一、冲突时如何产生的,以及制造一个冲突用来演示?

a.在项目经理的本地仓库新建一个Test.txt,并提交到本地仓库


Apple@DESKTOP-ECLNIU2 MINGW64 ~/Desktop/git basic operation/GitResp (master)
$ ll
total 2
-rw-r--r-- 1 Apple 197121 7 Jan 23 00:06 Demo.txt
-rw-r--r-- 1 Apple 197121 3 Jan 23 17:00 Demo2.txt

Apple@DESKTOP-ECLNIU2 MINGW64 ~/Desktop/git basic operation/GitResp (master)
$ ll
total 2
-rw-r--r-- 1 Apple 197121 7 Jan 23 00:06 Demo.txt
-rw-r--r-- 1 Apple 197121 3 Jan 23 17:00 Demo2.txt
-rw-r--r-- 1 Apple 197121 0 Jan 23 17:21 Test.txt

Apple@DESKTOP-ECLNIU2 MINGW64 ~/Desktop/git basic operation/GitResp (master)
$
Apple@DESKTOP-ECLNIU2 MINGW64 ~/Desktop/git basic operation/GitResp (master)
$ git add Test.txt

Apple@DESKTOP-ECLNIU2 MINGW64 ~/Desktop/git basic operation/GitResp (master)
$ git commit -m "在项目经理的本地仓库添加一个Test.txt,用于演示协同开发时的代码冲突" Test.txt
[master f060c6b] 在项目经理的本地仓库添加一个Test.txt,用于演示协同开发时的代码冲突
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 Test.txt

Apple@DESKTOP-ECLNIU2 MINGW64 ~/Desktop/git basic operation/GitResp (master)
$



b.然后从本地库推送到远程库

Apple@DESKTOP-ECLNIU2 MINGW64 ~/Desktop/git basic operation/GitResp (master)
$ git push origin master
Enumerating objects: 4, done.
Counting objects: 100% (4/4), done.
Delta compression using up to 16 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 398 bytes | 398.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
remote: Powered by GITEE.COM [GNK-6.2]
To https://gitee.com/gitee18261897435/git-resp.git
   bf506bf..f060c6b  master -> master

Apple@DESKTOP-ECLNIU2 MINGW64 ~/Desktop/git basic operation/GitResp (master)
$

c.当项目经理推送成功以后,程序员应该拉取下来
在这里插入图片描述
在另一台程序员的电脑进行拉取,到目前为止,远程合作没有问题
在这里插入图片描述

接下来操作同一个文件的同一个位置,演示代码冲突

d.程序员修改刚才拉取的Test.txt保存,然后添加到暂存区,提交到本地库,推送到远程库。
在这里插入图片描述

Administrator@wangj MINGW64 ~/Desktop/GitResp/git-resp (master)
$ git add Test.txt

Administrator@wangj MINGW64 ~/Desktop/GitResp/git-resp (master)
$ git commit -m "程序员修改了Test.txt,并且提交到本地库,然后推送到远程库,用于 演示协同开发时的代码冲突" Test.txt
[master ca4d2b4] 程序员修改了Test.txt,并且提交到本地库,然后推送到远程库,用于演示协同开发时的代码冲突
 1 file changed, 2 insertions(+)

Administrator@wangj MINGW64 ~/Desktop/GitResp/git-resp (master)
$ git push origin master
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 2 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 417 bytes | 417.00 KiB/s, done.
Total 3 (delta 1), reused 0 (delta 0), pack-reused 0
remote: Powered by GITEE.COM [GNK-6.2]
To https://gitee.com/gitee18261897435/git-resp.git
   f060c6b..ca4d2b4  master -> master

Administrator@wangj MINGW64 ~/Desktop/GitResp/git-resp (master)
$

程序员推送没有问题

e.项目经理也修改Test.txt,然后推送到远程库,造成冲突
在这里插入图片描述
在这里插入图片描述
这里就显示推送失败,原因也写的很清楚,有冲突需要推送者先将远程库拉取到本地,解决冲突,然后再推送.

查看远程库中的内容,显示该文件并没有被修改
在这里插入图片描述

二、解决冲突

1.项目经理先拉取到本地(通常谁需要推送到远程库有冲突,谁解决)

代码如下(示例):

Apple@DESKTOP-ECLNIU2 MINGW64 ~/Desktop/git basic operation/GitResp (master)
$ git pull origin master
remote: Enumerating objects: 5, done.
remote: Counting objects: 100% (5/5), done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 1), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), 397 bytes | 99.00 KiB/s, done.
From https://gitee.com/gitee18261897435/git-resp
 * branch            master     -> FETCH_HEAD
   f060c6b..ca4d2b4  master     -> origin/master
Auto-merging Test.txt
CONFLICT (content): Merge conflict in Test.txt
Automatic merge failed; fix conflicts and then commit the result.

Apple@DESKTOP-ECLNIU2 MINGW64 ~/Desktop/git basic operation/GitResp (master|MERGING)
$

使用git pull操作后,显示Auto-merging Test.txt

查看冲突文件,不知道为什么会乱码

在这里插入图片描述

2.人为修改Test.txt,解决冲突

在这里插入图片描述
通常在项目中在别人写的代码进行修改需要告知被修改的人
解决完冲突以后向服务器推送
注意:这里在commit的时候,合并代码不需要跟文件名字,否则commit不成功

如下(示例):

Apple@DESKTOP-ECLNIU2 MINGW64 ~/Desktop/git basic operation/GitResp (master|MERGING)
$ git add Test.txt

Apple@DESKTOP-ECLNIU2 MINGW64 ~/Desktop/git basic operation/GitResp (master|MERGING)
$ git commit -m "项目经理修改了Test.txt解决了冲突,然后重新提交推送到远程仓库" Test.txt
fatal: cannot do a partial commit during a merge.

Apple@DESKTOP-ECLNIU2 MINGW64 ~/Desktop/git basic operation/GitResp (master|MERGING)
$ ll
total 3
-rw-r--r-- 1 Apple 197121  7 Jan 23 00:06 Demo.txt
-rw-r--r-- 1 Apple 197121  3 Jan 23 17:00 Demo2.txt
-rw-r--r-- 1 Apple 197121 93 Jan 23 18:18 Test.txt

Apple@DESKTOP-ECLNIU2 MINGW64 ~/Desktop/git basic operation/GitResp (master|MERGING)
$ git commit -m "项目经理修改了Test.txt解决了冲突,然后重新提交推送到远程仓库"
[master fd1a1b6] 项目经理修改了Test.txt解决了冲突,然后重新提交推送到远程仓库

Apple@DESKTOP-ECLNIU2 MINGW64 ~/Desktop/git basic operation/GitResp (master)
$ git push origin master
Enumerating objects: 10, done.
Counting objects: 100% (10/10), done.
Delta compression using up to 16 threads
Compressing objects: 100% (5/5), done.
Writing objects: 100% (6/6), 852 bytes | 852.00 KiB/s, done.
Total 6 (delta 2), reused 0 (delta 0), pack-reused 0
remote: Powered by GITEE.COM [GNK-6.2]
To https://gitee.com/gitee18261897435/git-resp.git
   ca4d2b4..fd1a1b6  master -> master

Apple@DESKTOP-ECLNIU2 MINGW64 ~/Desktop/git basic operation/GitResp (master)
$


查看远程代码库,推送成功
在这里插入图片描述


总结

代码冲突即同一位置不同人员的推送造成,这时就需要人为解决冲突。然后再推送
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值