Home > Development Tools > git > body text

How to resolve merge conflicts in git

WBOY
Release: 2022-06-24 15:18:43
Original
33651 people have browsed it

Git method to resolve merge conflicts: 1. Edit the conflicting file, delete the special symbols in the file, and modify the code as needed; 2. Add the specified file to the staging area and add the specified branch Submit to the trunk and execute the commit. When using the "git commit" command, you cannot include a file name. If you add a file name, an error will be reported.

How to resolve merge conflicts in git

The operating environment of this article: Windows 10 system, Git version 2.30.0, Dell G3 computer.

How does git resolve merge conflicts

git conflicts

When multiple branch codes are merged into one branch, the same file is modified in both branches , will be generated no matter where the modification is made;

There is also a type that will be generated when the name of the same file is modified in two branches.

Cause

When merging branches, the two branches have two completely different sets of modifications in the same file. Git can't decide for

which one to use. The content of the new code must be decided manually.

Solution

Edit the conflicting file, delete the special symbols, and decide what content to use

Add to the temporary storage area

Execute submission (Note: You cannot use the git commit command with a file name at this time. Adding a file name will cause an error. After successful submission, merging will disappear)

Examples are as follows:

1. Conflict Generation

1.1, trunk branch code

There are two files in the trunk branch

Main.cpp

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

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

README.md

this is master branch
Copy after login

At this time, Tom and Jack pulled the code of the main branch and made modifications.

1.2. Tom modified the code and submitted it for merge

Tom created branch A and made the following modifications to the file

main.cpp

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

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

README.md

this is AAA branch
Copy after login

Submit the code and merge it into the trunk

lng@DESKTOP-9TD21KL MINGW64 ~/Desktop/tom/kaol (A)
$ git add .

lng@DESKTOP-9TD21KL MINGW64 ~/Desktop/tom/kaol (A)
$ git commit -m "A分支代码提交"
[A ccb2626] A分支代码提交
 2 files changed, 3 insertions(+), 3 deletions(-)

lng@DESKTOP-9TD21KL MINGW64 ~/Desktop/tom/kaol (A)
$ git push origin A
Enumerating objects: 7, done.
Counting objects: 100% (7/7), done.
Delta compression using up to 12 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (4/4), 376 bytes | 376.00 KiB/s, done.
Total 4 (delta 1), reused 3 (delta 1), pack-reused 0
remote: Powered by GITEE.COM [GNK-6.2]
remote: Create a pull request for &#39;A&#39; on Gitee by visiting:
remote:     https://gitee.com/lingpe/kaol/pull/new/lingpe:A...lingpe:master
To https://gitee.com/lingpe/kaol.git
 * [new branch]      A -> A

lng@DESKTOP-9TD21KL MINGW64 ~/Desktop/tom/kaol (A)
$ git checkout master
Switched to branch 'master'
Your branch is up to date with 'origin/master'.

lng@DESKTOP-9TD21KL MINGW64 ~/Desktop/tom/kaol (master)
$ git merge A
Updating 40c0115..ccb2626
Fast-forward
 README.md | 2 +-
 main.cpp  | 4 ++--
 2 files changed, 3 insertions(+), 3 deletions(-)

lng@DESKTOP-9TD21KL MINGW64 ~/Desktop/tom/kaol (master)
$ git push origin master
Total 0 (delta 0), reused 0 (delta 0), pack-reused 0
remote: Powered by GITEE.COM [GNK-6.2]
To https://gitee.com/lingpe/kaol.git
   40c0115..ccb2626  master -> master

lng@DESKTOP-9TD21KL MINGW64 ~/Desktop/tom/kaol (master)
$
Copy after login

1.3. Jack modified the code and submitted it for merge

jack made the following modifications to the code

main.cpp

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

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

README.md

this is BBB branch
Copy after login

Submit the code and merge it into the trunk

lng@DESKTOP-9TD21KL MINGW64 ~/Desktop/jack/kaol (B)
$ git add .

lng@DESKTOP-9TD21KL MINGW64 ~/Desktop/jack/kaol (B)
$ git commit -m "B分支代码提交"
[B bdcbe03] B分支代码提交
 2 files changed, 3 insertions(+), 3 deletions(-)

lng@DESKTOP-9TD21KL MINGW64 ~/Desktop/jack/kaol (B)
$ git push origin B
Enumerating objects: 53, done.
Counting objects: 100% (53/53), done.
Delta compression using up to 12 threads
Compressing objects: 100% (34/34), done.
Writing objects: 100% (50/50), 4.66 KiB | 2.33 MiB/s, done.
Total 50 (delta 16), reused 43 (delta 12), pack-reused 0
remote: Powered by GITEE.COM [GNK-6.2]
remote: Create a pull request for &#39;B&#39; on Gitee by visiting:
remote:     https://gitee.com/lingpe/kaol/pull/new/lingpe:B...lingpe:master
To https://gitee.com/lingpe/kaol.git
 * [new branch]      B -> B

lng@DESKTOP-9TD21KL MINGW64 ~/Desktop/jack/kaol (B)
$ git checkout master
Switched to branch 'master'
Your branch is up to date with 'origin/master'.

lng@DESKTOP-9TD21KL MINGW64 ~/Desktop/jack/kaol (master)
$ git merge B
Updating 40c0115..bdcbe03
Fast-forward
 README.md | 2 +-
 main.cpp  | 4 ++--
 2 files changed, 3 insertions(+), 3 deletions(-)

lng@DESKTOP-9TD21KL MINGW64 ~/Desktop/jack/kaol (master)
$
Copy after login

Conflicts occur when pushing

lng@DESKTOP-9TD21KL MINGW64 ~/Desktop/jack/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/jack/kaol (master)
$
Copy after login

2. Resolve conflicts

Continue The next step is how to resolve the conflict

Switch back to the B branch, and then pull the trunk branch code

lng@DESKTOP-9TD21KL MINGW64 ~/Desktop/jack/kaol (B)
$ git pull origin master
remote: Enumerating objects: 7, done.
remote: Counting objects: 100% (7/7), done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 4 (delta 1), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (4/4), 356 bytes | 178.00 KiB/s, done.
From https://gitee.com/lingpe/kaol
 * branch            master     -> FETCH_HEAD
   40c0115..ccb2626  master     -> origin/master
Auto-merging main.cpp
CONFLICT (content): Merge conflict in main.cpp
Auto-merging README.md
CONFLICT (content): Merge conflict in README.md
Automatic merge failed; fix conflicts and then commit the result.
Copy after login

If the pull fails, you can see a prompt message telling us which file caused the conflict.

Open the main.cpp file directly, you can see the following special characters, prompting us which line of code causes a conflict.

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

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

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

<<<<<<< HEAD
        printf("branch BBB\n");
=======
        printf("branch AAA\n");
>>>>>>> ccb26269f42245dfcbedfbf2218419c5ab7f2787

        return 0;
}
Copy after login

Resolve conflicts manually directly in the file. Remove special characters from the file and modify the code as needed.

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

int main()
{
        char data[100] = "my branch name is B and A";
        int length = strlen(data);

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

        printf("branch BBB\n");
        printf("branch AAA\n");
        return 0;
}
~
Copy after login

Similarly, for README.md, manually resolve conflicts.

this is BBB and AAA branch
Copy after login

After resolving the conflict, submit it to branch B

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

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

lng@DESKTOP-9TD21KL MINGW64 ~/Desktop/jack/kaol (B)
$ git push origin B
Enumerating objects: 10, done.
Counting objects: 100% (10/10), done.
Delta compression using up to 12 threads
Compressing objects: 100% (3/3), done.
Writing objects: 100% (4/4), 405 bytes | 405.00 KiB/s, done.
Total 4 (delta 1), reused 0 (delta 0), pack-reused 0
remote: Powered by GITEE.COM [GNK-6.2]
To https://gitee.com/lingpe/kaol.git
   bdcbe03..f30e1ea  B -> B

lng@DESKTOP-9TD21KL MINGW64 ~/Desktop/jack/kaol (B)
$
Copy after login

Finally merge branch B into the trunk, and there will be no conflict

lng@DESKTOP-9TD21KL MINGW64 ~/Desktop/jack/kaol (B)
$ git checkout master
Switched to branch 'master'
Your branch and 'origin/master' have perged,
and have 1 and 1 different commits each, respectively.
  (use "git pull" to merge the remote branch into yours)

lng@DESKTOP-9TD21KL MINGW64 ~/Desktop/jack/kaol (master)
$ git merge B
Updating bdcbe03..f30e1ea
Fast-forward
 README.md | 2 +-
 main.cpp  | 4 ++--
 2 files changed, 3 insertions(+), 3 deletions(-)

lng@DESKTOP-9TD21KL MINGW64 ~/Desktop/jack/kaol (master)
$ git push origin master
Total 0 (delta 0), reused 0 (delta 0), pack-reused 0
remote: Powered by GITEE.COM [GNK-6.2]
To https://gitee.com/lingpe/kaol.git
   ccb2626..f30e1ea  master -> master

lng@DESKTOP-9TD21KL MINGW64 ~/Desktop/jack/kaol (master)
$
Copy after login

At this point, the conflict is successfully resolved

You can look at the code of the trunk branch

main.cpp

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

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

README.md

this is BBB and AAA branch
Copy after login

OK

Recommended study: "Git tutorial

The above is the detailed content of How to resolve merge conflicts in git. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
git
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template