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

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

ขณะใช้ git ติดตามการเปลี่ยนแปลงโปรเจกต์หลายอย่างอาจทำให้ error เมื่อย้าย branch อาจเจอ error:

error: the following untracked working tree files would be overwritten by checkout [ List of Files Here ]
Please, commit your changes or stash them before you can switch branches.

Error เกิดเมื่อมีไฟล์บน current branch ที่มี changes บน branch ที่ทำงานอยู่ด้วย วิธีแก้ง่าย: ทำตามข้อความสุดท้ายของ error

Commit Changes (Commit การเปลี่ยนแปลง)

Commit changes ด้วย:

git commit -m "Insert meaningful git commit message here"

ควร clean working tree แล้วใช้ git checkout <name-of-branch> สลับ branch

Stash Changes (Stash การเปลี่ยนแปลง)

ใช้ git stash เก็บการเปลี่ยนแปลงไว้จนกว่าจะพร้อม commit Git stash conserve changes แต่ไม่ associate กับ commit หรือ branch จนกว่าพร้อม

วิธีใช้ stash ใน local repository:

git stash push
git checkout <branch-you-need-to-switch-to>
--- do whatever you have to do on <branch-you-need-to-switch-to> ---
git checkout <previous-branch>
git stash pop

หากคุ้นเคย stack (LIFO) จะเข้าใจง่าย latest stash เพิ่มท้าย list pop ออกเมื่อพร้อมใช้

Default behavior: pop ออก last element ใน stash list และ remove มันหมด หากไม่อยากกำจัด stash แค่ใช้ ใช้ git stash apply

สรุป

บทความดู 2 วิธีแก้ working tree file error ที่จะถูก overwrite on checkout ส่วนใหญ่ทำตาม git error บอกได้ ทั้ง commit changes หรือ stash changes เพื่อ clear working tree สลับ branch วิธีไหนขึ้นอยู่กับ git workflow ของทีม ถ้าไม่แน่ใจขอช่วยเหลือ Happy hacking!