git解決合併衝突的方法:1、編輯有衝突的文件,並刪除文件中的特殊符號,根據需求修改程式碼;2、將指定文件新增至暫存區,並將指定的分支提交到主幹,執行提交即可,其中使用「git commit」指令時不能帶檔名, 加檔名會報錯。
本文操作環境:Windows10系統、Git2.30.0版、Dell G3電腦。
git衝突
多個分支程式碼合併到一個分支時,兩個分支中修改了同一個文件,不管是什麼地方修改,都會產生;
還有一種兩個分支中修改了同一個檔案的名稱時會產生。
原因
合併分支時,兩個分支在同一個檔案有兩套完全不同的修改。 Git 無法替
我們決定要使用哪一個。必須人為決定新程式碼內容。
解決方法
編輯有衝突的文件,刪除特殊符號,決定要使用的內容
加入到暫存區
執行提交(注意:此時使用git commit 指令時不能帶檔名, 加檔名會報錯,成功提交後,merging消失)
範例如下:
在主幹分支有兩個檔案
## 這時候tom和jack分別拉取了主幹分支的程式碼,並進行了修改。
1.2、tom修改程式碼提交合併
tom建立A分支並對檔案做了以下修改
#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
提交程式碼並合併到主幹
#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; }
1.3、jack修改程式碼提交合併
jack對程式碼做了以下修改
this is AAA branch
README.md
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) $
提交程式碼並合併至主幹
#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; }
push時產生衝突
this is BBB branch
2、解決衝突
接下來就是如何解決衝突
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) $
拉取失敗,可以看到提示訊息,告訴我們哪個檔案產生了衝突。
直接開啟main.cpp文件,可以看到有以下特殊字符,提示我們哪一行程式碼產生了衝突。
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) $
直接在檔案中手動解決衝突。刪除檔案中的特殊字符,然後根據需求修改代碼。
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.
同理,對README.md,手動解決衝突。
#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; }
解決完衝突後提交到B分支
#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; } ~
最後將B分支合併到主幹,就不會產生衝突了
this is BBB and AAA branch
至此,衝突成功解決
可以看下主幹分支的程式碼
main.cpp
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) $
README.md
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) $
OK
推薦學習:《
Git教程》
以上是git怎麼解決合併衝突的詳細內容。更多資訊請關注PHP中文網其他相關文章!