我们在同一分支上开发代码时,代码冲突是不可避免的。下面通过一个简单的例子说下如何解决冲突
先看下分支上的代码
#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
至此,冲突成功解决。