git提交代码到同一分支时冲突的解决

我们在同一分支上开发代码时,代码冲突是不可避免的。下面通过一个简单的例子说下如何解决冲突

先看下分支上的代码

#include <stdio.h>
#include <string.h>

int main()
{
	char data[100] = {0};
	int length = strlen(data);
	
	for(int i = 0; i < length; i++)
	{
		printf("%c", data[i]);
	}
	
	printf("\n");
	
	return 0;
}

A用户修改代码,并提交。

#include <stdio.h>
#include <string.h>

int main()
{
	char data[100] = "my name is A";
	int length = strlen(data);
	
	for(int i = 0; i < length; i++)
	{
		printf("%c", data[i]);
	}
	
	printf("\n");
	
	return 0;
}

B用户修改代码

#include <stdio.h>
#include <string.h>

int main()
{
	char data[100] = "my name is B";
	int length = strlen(data);
	
	for(int i = 0; i < length; i++)
	{
		printf("%c", data[i]);
	}
	
	printf("\n");
	
	return 0;
}

当B用户git push时,就会出现冲突,下面是报错信息。

lng@DESKTOP-9TD21KL MINGW64 ~/Desktop/kaol/kaol (master)
$ git push origin master
To https://gitee.com/lingpe/kaol.git
 ! [rejected]        master -> master (fetch first)
error: failed to push some refs to 'https://gitee.com/lingpe/kaol.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

接下来我们需要需要先拉取分支的代码

lng@DESKTOP-9TD21KL MINGW64 ~/Desktop/kaol/kaol (master)
$ git pull
remote: Enumerating objects: 5, done.
remote: Counting objects: 100% (5/5), done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 3 (delta 1), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), 308 bytes | 51.00 KiB/s, done.
From https://gitee.com/lingpe/kaol
   1bb7c56..65d76a9  master     -> origin/master
Auto-merging main.cpp
CONFLICT (content): Merge conflict in main.cpp
Automatic merge failed; fix conflicts and then commit the result.

可以看到如图提示信息,告诉我们哪个文件发生了冲突

我们打开文件,可以看到文件中有一些特殊字符,会提示具体是哪一行代码发生了冲突。

#include <stdio.h>
#include <string.h>

int main()
{
<<<<<<< HEAD
        char data[100] = "my name is B";
=======
        char data[100] = "my name is A";
>>>>>>> 65d76a9f8ffabbd5983b101ca2b02b31bc854fb8
        int length = strlen(data);

        for(int i = 0; i < length; i++)
        {
                printf("%c", data[i]);
        }

        printf("\n");

        return 0;
}

我们删除文件中的特殊字符,再根据需求决定要删除或保留哪些代码

#include <stdio.h>
#include <string.h>

int main()
{
        char data1[100] = "my name is B";
        char data2[100] = "my name is A";
        int length = strlen(data1);

        for(int i = 0; i < length; i++)
        {
                printf("%c", data1[i]);
        }

        printf("\n");

        return 0;
}

修改完成后重新提交

lng@DESKTOP-9TD21KL MINGW64 ~/Desktop/kaol/kaol (master|MERGING)
$ git add .

lng@DESKTOP-9TD21KL MINGW64 ~/Desktop/kaol/kaol (master|MERGING)
$ git commit -m "解决冲突"
[master 32ffcce] 解决冲突

lng@DESKTOP-9TD21KL MINGW64 ~/Desktop/kaol/kaol (master)
$ git push origin master
Enumerating objects: 10, done.
Counting objects: 100% (10/10), done.
Delta compression using up to 12 threads
Compressing objects: 100% (6/6), done.
Writing objects: 100% (6/6), 671 bytes | 671.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/lingpe/kaol.git
   65d76a9..32ffcce  master -> master

至此,冲突成功解决。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

大草原的小灰灰

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值