Home > Article > Development Tools > How to resolve merge conflicts in git
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.
The operating environment of this article: Windows 10 system, Git version 2.30.0, Dell G3 computer.
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:
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; }
README.md
this is master branch
At this time, Tom and Jack pulled the code of the main branch and made modifications.
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; }
README.md
this is AAA branch
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 'A' 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) $
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; }
README.md
this is BBB branch
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 'B' 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) $
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) $
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.
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; }
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; } ~
Similarly, for README.md, manually resolve conflicts.
this is BBB and AAA branch
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) $
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) $
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; }
README.md
this is BBB and AAA branch
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!