Kaedah Git untuk menyelesaikan konflik gabungan: 1. Edit fail bercanggah, padam simbol khas dalam fail dan ubah suai kod seperti yang diperlukan 2. Tambah fail yang ditentukan ke kawasan pementasan dan tambah cawangan yang ditentukan Serahkan ke trunk dan laksanakan komit Apabila menggunakan arahan "git commit", anda tidak boleh memasukkan nama fail Jika anda menambah nama fail, ralat akan dilaporkan.
Persekitaran pengendalian artikel ini: sistem Windows 10, Git versi 2.30.0, komputer Dell G3.
konflik git
Apabila berbilang kod cawangan digabungkan menjadi satu cawangan, fail yang sama diubah suai dalam kedua-dua branches , akan dijana tidak kira di mana pengubahsuaian dibuat;
Terdapat juga jenis yang akan dijana apabila nama fail yang sama diubah suai dalam dua cawangan.
Sebab
Apabila menggabungkan cawangan, kedua-dua cawangan mempunyai dua set pengubahsuaian yang berbeza sama sekali dalam fail yang sama. Git tidak boleh memutuskan yang mana satu untuk digunakan untuk
kami. Kandungan kod baharu mesti diputuskan secara manual.
Penyelesaian
Edit fail yang bercanggah, padamkan simbol khas dan tentukan kandungan yang hendak digunakan
Tambahkan pada kawasan pementasan
Laksanakan penyerahan (Nota: Anda tidak boleh menggunakan arahan git commit dengan nama fail pada masa ini. Menambah nama fail akan menyebabkan ralat. Selepas penyerahan berjaya, penggabungan akan hilang)
Contohnya adalah seperti berikut:
Terdapat dua fail dalam cawangan batang
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
Pada masa ini, Tom dan Jack menarik kod cawangan batang dan membuat pengubahsuaian.
tom mencipta cawangan A dan membuat pengubahsuaian berikut pada fail
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
Serahkan kod dan gabungkannya ke dalam batang
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) $
bicu membuat pengubahsuaian berikut pada kod
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
Komitkan kod dan gabungkannya ke dalam batang
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) $
Konflik berlaku apabila menolak
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) $
Langkah seterusnya ialah cara menyelesaikan konflik
Tukar semula kepada B. cawangan, dan kemudian tarik kod cawangan utama
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.
Jika tarikan gagal, anda boleh Kami akan melihat mesej gesaan memberitahu kami fail yang menyebabkan konflik.
Buka fail main.cpp secara langsung Anda boleh melihat aksara khas berikut, yang menggesa kami baris kod yang menyebabkan konflik.
#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; }
Selesaikan konflik secara manual terus dalam fail. Alih keluar aksara khas daripada fail dan ubah suai kod seperti yang diperlukan.
#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; } ~
Begitu juga, untuk README.md, selesaikan konflik secara manual.
this is BBB and AAA branch
Selepas menyelesaikan konflik, serahkannya kepada cawangan 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) $
Akhirnya gabungkan cawangan B ke dalam batang, dan tidak akan ada konflik
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) $
Itu sahaja , konflik berjaya diselesaikan
Anda boleh lihat kod cawangan utama
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
Pembelajaran yang disyorkan: "Tutorial Git"
Atas ialah kandungan terperinci Bagaimana untuk menyelesaikan konflik gabungan dalam git. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!