Error ด้านบนมัก Triggered เมื่อเราไม่ได้ Clone Repository ที่เราพยายาม Pull โปรเจกต์อาจเหมือนกัน แต่เราอาจทำงาน Local หนึ่งขณะพยายาม Pull จาก Repo บน GitHub เพราะอาจมีไฟล์หรือ Features อื่นที่เราอยาก Incorporate บน Local Version
มีหลายวิธีแก้ไขนี้
บอก Local Branch Track Remote Branch
เราสามารถทำได้ด้วยคำสั่งด้านล่าง:
git branch --track <branch-name> origin/<branch-name>
ทำอย่างนี้ Remote Branch จะกลายเป็น Counterpart ของ Remote Server หลังจากนี้ ทำ git status ดู Difference ระหว่าง 2 Repos
Staging และ Stashing
เพื่อ Pull ให้ Local Files ไม่ถูก Overwrite จาก Version Control เราสามารถ Stage แล้ว Stash ด้วยคำสั่งด้านล่าง:
git add -A git stash git pull
ด้วย git add -A เรา Stage ทุก Changes เหมือน git add ที่ Look at all Working Trees และ Adds all Working Paths to Staged Changes, Changed, New, หรือ Not Ignored นอกจากนี้ ยังทำหน้าที่เหมือน git add -u ที่ Look at Files ที่ Tracked อยู่แล้ว และ Stage Changes ของไฟล์เหล่านั้นหาก Removed หรือ Differ
ด้วย git stash เรานำ Staged และ Unstaged Uncommitted Changes Stash ไว้ Future Use แล้ว Revert จาก Working Copy หลังจากนี้ เรอิสระทำ Changes เช่น Pulling New Files
Fetching และ Resetting
หากทั้ง 2 ข้างบนยังไม่ได้ผล ลอง Fetching และ Resetting เนื่องจากเราจะใช้ --hard Option นี้ สำคัญที่จะต้องลอง 2 วิธีก่อน เพราะ --hard เป็น Command ที่อันตราย Potentially ซึ่ง Throw Away Uncommitted Changes ทั้งหมด
โปรดทำ git status ก่อนลอง Fix ด้านล่าง เพื่อให้แน่ใจว่า Output ว่าง
git fetch --all git reset --hard origin/<branch-name>
ด้วย git fetch --all เรา Fetch ทุก Remote Branches Fetch จะ Update Local Copies ของ Remote Branches แต่จะไม่ Update Local Branches ที่ Track Remote Branches เพื่อ Achieve นี้ เราต้องทำ git pull --all
ด้วย git reset --hard origin/<branch-name> เราพูดว่า "Throw Everything Away ที่อยู่ใน Local Branch ของฉัน และทำให้เหมือน Remote Branch" มัน Throw Away Staged และ Unstaged Changes ทั้งหมด
สรุป
Error: the following untracked working tree files would be overwritten by merge Triggered เมื่อเราพยายาม Pull Remote Branch ขณะอยู่บน Local Branch โปรเจกต์อาจเหมือนกัน แต Local ต้อง Track Remote ได้เพื่อ Pull สำเร็จ
Error นี้มัก Triggered เมื่อ Developer ลืม Clone Repo วิธีแก้อื่นๆ คือ Staging และ Stashing หรือ Fetching และ Resetting ซึ่งควรลองก็ต่อเมื่อ 2 วิธีแรกไม่สำเร็จ