git如何解决合并冲突
这篇文章主要介绍“git如何解决合并冲突”,在日常操作中,相信很多人在git如何解决合并冲突问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”git如何解决合并冲突”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!
git解决合并冲突的方法:1、编辑有冲突的文件,并删除文件中的特殊符号,根据需求修改代码;2、将指定文件添加到暂存区,并将指定的分支提交到主干,执行提交即可,其中使用“git commit”命令时不能带文件名, 加文件名会报错。
本文操作环境:Windows10系统、Git2.30.0版、Dell G3电脑。
git解决合并冲突
git冲突
多个分支代码合并到一个分支时,两个分支中修改了同一个文件,不管是什么地方修改,都会产生;
还有一种 两个分支中修改了同一个文件的名称时会产生。
原因
合并分支时,两个分支在同一个文件有两套完全不同的修改。Git 无法替
我们决定使用哪一个。必须人为决定新代码内容。
解决方法
编辑有冲突的文件,删除特殊符号,决定要使用的内容
添加到暂存区
执行提交(注意:此时使用 git commit 命令时不能带文件名, 加文件名会报错,成功提交后,merging消失)
示例如下:
1、冲突的产生
1.1、主干分支代码
在主干分支有两个文件
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
这个时候tom和jack分别拉取了主干分支的代码,并进行了修改。
1.2、tom修改代码提交合并
tom创建A分支并对文件做了以下修改
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
提交代码并合并到主干
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) $
1.3、jack修改代码提交合并
jack对代码做了如下修改
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
提交代码并合并到主干
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) $
push时产生冲突
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) $
2、解决冲突
接下来就是如何解决冲突
切换回B分支,然后拉取主干分支代码
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.
拉取失败,可以看到提示信息,告诉我们哪个文件产生了冲突。
直接打开main.cpp文件,可以看到有以下特殊字符,提示我们哪一行代码产生了冲突。
#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; }
直接在文件中手动解决冲突。删除文件中的特殊字符,然后根据需求修改代码。
#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
解决完冲突后提交到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) $
最后将B分支合并到主干,就不会产生冲突了
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) $
至此,冲突成功解决
可以看下主干分支的代码
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
到此,关于“git如何解决合并冲突”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注蜗牛博客网站,小编会继续努力为大家带来更多实用的文章!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:niceseo99@gmail.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。
评论