การ รวมสาขา Git
รวมสาขา
เรามีโปรแกรมแก้ไขฉุกเฉินพร้อมแล้ว เรามารวมสาขาหลักและส่วนแก้ไขฉุกเฉินเข้าด้วยกันกันเถอะ
ก่อนอื่นเราต้องเปลี่ยนเป็นสาขาหลัก:
ตัวอย่าง
git checkout master
Switched to branch 'master'
ตอนนี้เรารวมสาขาปัจจุบัน (หลัก) ด้วยการแก้ไขฉุกเฉิน:
ตัวอย่าง
git merge emergency-fix
Updating 09f4acd..dfa79db
Fast-forward
index.html | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
เนื่องจากสาขาการแก้ไขฉุกเฉินส่งตรงจากอาจารย์ และไม่มีการเปลี่ยนแปลงอื่นใดกับอาจารย์ในขณะที่เรากำลังทำงาน Git เห็นว่านี่เป็นความต่อเนื่องของอาจารย์ จึงสามารถ "กรอไปข้างหน้า" เพียงแค่ชี้ทั้งหลักและการแก้ไขฉุกเฉินไปที่การคอมมิตเดียวกัน
เนื่องจากการแก้ไขหลักและการแก้ไขฉุกเฉินโดยพื้นฐานแล้วในตอนนี้ เราสามารถลบโปรแกรมแก้ไขฉุกเฉินได้ เนื่องจากไม่จำเป็นอีกต่อไป:
ตัวอย่าง
git branch -d emergency-fix
Deleted branch emergency-fix (was dfa79db).
ผสานความขัดแย้ง
ตอนนี้เราสามารถย้ายไปที่ Hello-world-image และทำงานต่อไปได้ เพิ่มไฟล์รูปภาพอื่น (img_hello_git.jpg) และเปลี่ยน index.html เพื่อให้แสดง:
ตัวอย่าง
git checkout hello-world-images
Switched to branch 'hello-world-images'
ตัวอย่าง
<!DOCTYPE html>
<html>
<head>
<title>Hello World!</title>
<link rel="stylesheet" href="bluestyle.css">
</head>
<body>
<h1>Hello world!</h1>
<div><img src="img_hello_world.jpg" alt="Hello World
from Space" style="width:100%;max-width:960px"></div>
<p>This is the first
file in my new Git Repo.</p>
<p>A new line in our file!</p>
<div><img
src="img_hello_git.jpg" alt="Hello Git"
style="width:100%;max-width:640px"></div>
</body>
</html>
ตอนนี้ เราทำงานที่นี่เสร็จแล้ว และสามารถดำเนินการสำหรับสาขานี้ได้:
ตัวอย่าง
git add --all
git commit -m "added new image"
[hello-world-images 1f1584e] added new image
2 files changed, 1 insertion(+)
create mode 100644 img_hello_git.jpg
เราจะเห็นว่า index.html มีการเปลี่ยนแปลงทั้งสองสาขา ตอนนี้เราพร้อมที่จะรวมภาพโลกสวัสดีเข้าเป็นต้นแบบแล้ว แต่จะเกิดอะไรขึ้นกับการเปลี่ยนแปลงที่เราเพิ่งทำในมาสเตอร์
ตัวอย่าง
git checkout master
git merge hello-world-images
Auto-merging index.html
CONFLICT (content): Merge conflict in index.html
Automatic merge failed; fix conflicts and then commit the result.
การผสานล้มเหลว เนื่องจากมีข้อขัดแย้งระหว่างเวอร์ชันสำหรับ index.html ให้เราตรวจสอบสถานะ:
ตัวอย่าง
git status
On branch master
You have unmerged paths.
(fix conflicts and run "git commit")
(use "git merge --abort" to abort the merge)
Changes to be committed:
new file: img_hello_git.jpg
new file: img_hello_world.jpg
Unmerged paths:
(use "git add <file>..." to mark resolution)
both modified: index.html
สิ่งนี้เป็นการยืนยันว่ามีข้อขัดแย้งใน index.html แต่ไฟล์รูปภาพพร้อมและแสดงเป็นฉากเพื่อคอมมิต
เราจึงต้องแก้ไขข้อขัดแย้งนั้น เปิดไฟล์ในตัวแก้ไขของเรา:
ตัวอย่าง
<!DOCTYPE html>
<html>
<head>
<title>Hello World!</title>
<link
rel="stylesheet" href="bluestyle.css">
</head>
<body>
<h1>Hello
world!</h1>
<div><img src="img_hello_world.jpg" alt="Hello World from
Space" style="width:100%;max-width:960px"></div>
<p>This is the first file
in my new Git Repo.</p>
<<<<<<< HEAD
<p>This line is here to show how
merging works.</p>
=======
<p>A new line in our file!</p>
<div><img
src="img_hello_git.jpg" alt="Hello Git"
style="width:100%;max-width:640px"></div>
>>>>>>> hello-world-images
</body>
</html>
เราสามารถเห็นความแตกต่างระหว่างเวอร์ชันต่างๆ และแก้ไขได้ตามต้องการ:
ตัวอย่าง
<!DOCTYPE html>
<html>
<head>
<title>Hello World!</title>
<link
rel="stylesheet" href="bluestyle.css">
</head>
<body>
<h1>Hello
world!</h1>
<div><img src="img_hello_world.jpg" alt="Hello World from
Space" style="width:100%;max-width:960px"></div>
<p>This is the first file
in my new Git Repo.</p>
<p>This line is here to show how
merging works.</p>
<div><img
src="img_hello_git.jpg" alt="Hello Git"
style="width:100%;max-width:640px"></div>
</body>
</html>
ตอนนี้เราสามารถแสดง index.html และตรวจสอบสถานะได้:
ตัวอย่าง
git add index.html
git status
On branch master
All conflicts fixed but you are still merging.
(use "git commit" to conclude merge)
Changes to be committed:
new file: img_hello_git.jpg
new file: img_hello_world.jpg
modified: index.html
ความขัดแย้งได้รับการแก้ไขแล้ว และเราสามารถใช้คอมมิตเพื่อสรุปการรวม:
ตัวอย่าง
git commit -m "merged with hello-world-images after fixing conflicts"
[master e0b6038] merged with hello-world-images after fixing conflicts
และลบสาขา hello-world-image:
ตัวอย่าง
git branch -d hello-world-images
Deleted branch hello-world-images (was 1f1584e).
ตอนนี้คุณมีความเข้าใจที่ดีขึ้นเกี่ยวกับวิธีการทำงานของสาขาและการควบรวมกิจการ ถึงเวลาเริ่มทำงานกับที่เก็บระยะไกลแล้ว!