คุณไม่สามารถ Pull Code จาก Remote Repository ได้หากมีความขัดแย้งระหว่าง Uncommitted Changes ที่คุณทำบนเครื่อง Local และเนื้อหาของ Remote Repository นี่คือการปกป้องไม่ให้คุณเขียนทับโค้ดที่ต้องการเก็บ
ในคู่มือนี้ เราจะอธิบาย Error "Your local changes to the following files would be overwritten by merge" และเหตุผลที่ Error ถูก Raise พร้อมตัวอย่างเพื่อเรียนรู้วิธีแก้ไข
Your local changes to the following files would be overwritten by merge
Local Version ของ Repository บ่อยครั้งจะต่างจาก Remote Repository นี่คือ Core Feature ของ Git: คุณสามารถทำการเปลี่ยนแปลง Local ได้โดยไม่ Push ไป Remote จนกว่าจะพร้อม
เมื่อ Pull Code จาก Remote Repository Git จะดึงเนื้อหาของ Repository นั้นมาเก็บที่เครื่อง Local สามารถทำได้ก็ต่อเมื่อคุณ Commit การเปลี่ยนแปลงของไฟล์ที่ต้องการเก็บทั้งหมดแล้ว
หากลอง Pull Code โดยไม่ Commit Changes ก่อน อาจเจอ Error "Your local changes to the following files would be overwritten by merge" จะเกิดขึ้นก็ต่อเมื่อคุณ Modify ไฟล์ที่ถูกเปลี่ยนแปลงบน Remote Version ด้วย
ตัวอย่างสถานการณ์
ลองทำ Revision บน Git Repository ชื่อ ck-git เริ่มจาก Clone Repository:
git clone https://github.com/career-karma-tutorials/ck-git
ตอนนี้เรามี Copy ของ Repository บน Local Repository นี้มีไฟล์เดียว: README.md เนื้อหาคือ:
# ck-git
เราไปที่ GitHub แก้ไขไฟล์นี้บน Server ตอนนี้ไฟล์มีเนื้อหา:
# Career Karma Git
Local Version ของเราตอนนี้ต่างจาก Origin Master Branch บน Remote Version
Version ของเรามีโค้ดที่อยู่ใน Remote ตอนเรา Pull โค้ดมา Remote Repository มีการเปลี่ยนแปลงที่เราทำไปที่ README.md
ตอนนี้ สมมติเราแก้ไข README.md บนเครื่อง Local:
# CK Git
เราเปลี่ยนไฟล์ของเรา ตอนนี้ลอง Pull การเปลี่ยนแปลงที่เราทำไปที่ Remote Repository เพื่อบันทึกไว้ที่ Local:
git pull
คำสั่งคืน Error:
... Updating a30b784..ec281fc error: Your local changes to the following files would be overwritten by merge: README.md Please commit your changes or stash them before you merge. Aborting
เราย่อ Response ไว้เพื่อความกระชับ ข้อความที่ควรโฟกัสคือ "Your local changes to the following files would be overwritten by merge"
วิธีแก้ไข
ตอนนี้มี 2 Copy ของไฟล์: ตัวบน Local และตัวใน Remote Repository เมื่อ Pull Code จาก Remote Git ไม่แน่ใจว่า Version ไหนควรคงไว้
เพื่อแก้ไข Error นี้ เราสามารถ Stash โค้ดหรือ Commit โค้ดได้
เพื่อ Commit โค้ด เราสามารถ Add README.md เข้า Commit และสร้าง Commit ที่มีไฟล์นั้น:
git add README.md git commit -m "docs: Update README.md"
นี่จะเพิ่มการเปลี่ยนแปลงที่เราทำที่ README.md บน Local เข้า Commit History ของโปรเจกต์ Git จะรู้ว่าเราต้องการเก็บการเปลี่ยนแปลงเหล่านี้
วิธีที่สองคือ Stash การเปลี่ยนแปลง ให้เราบันทึกการเปลี่ยนแปลงไว้ดูภายหลัง สามารถ Stash ได้ด้วย git stash:
git stash
คำสั่งนี้บันทึกการเปลี่ยนแปลงของเราไว้ใน Stash (โค้ดถูก "stashed away" ไว้ภายหลัง) ตอนที่เรา Stash แล้ว เราสามารถ Pull Code จาก Remote Repository ได้ปลอดภัย:
git stash pop
โค้ดใน Git Stash สามารถดูได้โดยรัน git stash pop จะให้เราเห็นการเปลี่ยนแปลงที่เราทำไปที่ไฟล์ที่ไม่ได้ Commit เข้า Repository หมายความว่าหากเราตัดสินใจ Commit การเปลี่ยนแปลงภายหลัง เราก็สามารถทำได้
สรุป
Error "Your local changes to the following files would be overwritten by merge" เกิดขึ้นเมื่อพยายาม Pull Remote Repository มาที่ Local Machine ที่เนื้อหาขัดแย้งกับ Local Version
เพื่อแก้ไข Error นี้ ให้ Stash การเปลี่ยนแปลงไว้ภายหลัง หรือ Commit การเปลี่ยนแปลง ตอนนี้คุณมีความรู้เพียงพอที่จะแก้ไข Error นี้ได้แล้ว!