Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> การเขียนโปรแกรม

แก้ไขข้อผิดพลาด Git: Local changes would be overwritten by checkout

คุณไม่สามารถแก้ไขไฟล์เดียวกันบนสอง branches แล้วสลับ branch ระหว่างกันได้โดยไม่ commit หรือ stash ไฟล์นั้น เนื่องจาก Git ไม่รู้ว่าจะเก็บการเปลี่ยนแปลงไว้ หรือทับทิ้ง

คู่มือนี้อธิบาย error Your local changes to the following files would be overwritten by checkout และวิธีแก้ไขพร้อมตัวอย่าง

Your local changes to the following files would be overwritten by checkout

Git version control ให้ maintain เส้นทางพัฒนาแยกกัน (branches) การเปลี่ยนแปลงบน branch หนึ่งไม่สะท้อนบนอีก branch จนกว่าจะ merge

เมื่อ checkout ไป branch ใด จะเห็น repository ณ เวลานั้นและทำการเปลี่ยนแปลงได้ ควร commit เมื่อพร้อม เพื่อ Git ติดตามวิวัฒนาการโปรเจกต์

ไม่สามารถสลับระหว่างสอง branches ได้ หากทั้งสอง branches มีการเปลี่ยนแปลงไฟล์เดียวกันที่ยังไม่ commit Git ต้องรู้ว่าไฟล์ควรบันทึกหรือเป็นส่วนของ commit เพื่อป้องกันการทับทิ้งโค้ดที่อาจต้องอ้างอิงกลับ

ตัวอย่างสถานการณ์ (An Example Scenario)

Clone repository มีไฟล์ README.md:

git clone https://github.com/career-karma-tutorials/ck-git

เนื้อหา README.md:

# ck-git

Repository มี 2 branches: development และ master ใน local working directory แก้ไข README.md บน origin master branch:

# Career Karma Git

ตอนนี้ master branch ต่างจาก remote repository จะ checkout ไป development branch และแก้ไข README ด้วย:

# Career Karma [Development]

เปลี่ยนไฟล์บน development branch ด้วย:

git checkout development
nano README.md

กลับไป master branch commit และ push:

git checkout master
git add README.md
git commit -m "docs: Update README"
git push

ตอนนี้ remote repository มี:

  • README.md ที่แก้ไขบน development branch
  • master branch นำหน้า 1 commit

แก้ไข README.md อีกครั้งบน master branch:

# Tutorials

ตอนนี้ทั้งสอง branches มีความแตกต่างจาก remote master branch ลอง checkout ไป development branch:

git checkout development

เกิด error:

error: Your local changes to the following files would be overwritten by checkout: README.md
Please commit your changes or stash them before you switch branches.
Aborting

วิธีแก้ไข (The Solution)

เพราะ development และ master branches มี uncommitted changes Git จึงไม่ checkout ได้ หาก checkout จะสูญเสีย uncommitted changes บน master branch

แก้ได้ 2 วิธี:

1. Commit การเปลี่ยนแปลงบน master branch

git add README.md
git commit -m "docs: Add tutorials message to README.md file"
git push

คำสั่ง add README.md เข้า staging area, commit ทั้งหมดจาก staging area, push ไป remote repository ตอนนี้ Git มี record ของ README.md พร้อมการเปลี่ยนแปลง

2. Stash การเปลี่ยนแปลงไว้ก่อน (Stash Changes)

เหมาะหากอยากเก็บการเปลี่ยนแปลงไว้ใช้ภายหลัง แต่ยังไม่พร้อม commit ใช้ git stash:

git stash save README.md

จะ save README.md ไว้ใน stash เมื่อพร้อมกลับมา ใช้ git stash pop:

git stash pop

คำสั่งคืนค่า README.md กลับมาใน repository "Stashing" คือ "เก็บไว้ก่อน" Stashes ไม่สร้าง commits

หลังรันวิธีแก้ไขใดวิธีหนึ่ง สามารถ checkout ไป development branch ได้:

git checkout development

คำสั่งสลับ branch แจ้งว่า switched to branch 'development' แก้ไขสำเร็จ

สรุป

Error Your local changes to the following files would be overwritten by checkout เกิดเมื่อแก้ไขบน 2 branches โดยไม่ commit หรือ stash แล้วพยายามสลับ branch

แก้ไขได้โดย stash ไว้ก่อน หรือ commit เข้า repository ตอนนี้คุณมีความรู้แก้ error นี้อย่างมืออาชีพแล้ว!