本文操作环境: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 masterlng@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 benumerating objects: 53, done.counting objects: 100% (53/53), done.delta compression using up to 12 threadscompressing 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 0remote: 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:masterto https://gitee.com/lingpe/kaol.git * [new branch] b -> blng@desktop-9td21kl mingw64 ~/desktop/jack/kaol (b)$ git checkout masterswitched to branch 'master'your branch is up to date with 'origin/master'.lng@desktop-9td21kl mingw64 ~/desktop/jack/kaol (master)$ git merge bupdating 40c0115..bdcbe03fast-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 masterto 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 dohint: not have locally. this is usually caused by another repository pushinghint: to the same ref. you may want to first integrate the remote changeshint: (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 masterremote: 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 0unpacking 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/masterauto-merging main.cppconflict (content): merge conflict in main.cppauto-merging readme.mdconflict (content): merge conflict in readme.mdautomatic 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 benumerating objects: 10, done.counting objects: 100% (10/10), done.delta compression using up to 12 threadscompressing 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 0remote: powered by gitee.com [gnk-6.2]to https://gitee.com/lingpe/kaol.git bdcbe03..f30e1ea b -> blng@desktop-9td21kl mingw64 ~/desktop/jack/kaol (b)$
最后将b分支合并到主干,就不会产生冲突了
lng@desktop-9td21kl mingw64 ~/desktop/jack/kaol (b)$ git checkout masterswitched 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 bupdating bdcbe03..f30e1eafast-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 mastertotal 0 (delta 0), reused 0 (delta 0), pack-reused 0remote: powered by gitee.com [gnk-6.2]to https://gitee.com/lingpe/kaol.git ccb2626..f30e1ea master -> masterlng@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
ok
推荐学习:《git教程》
以上就是git怎么解决合并冲突的详细内容。
