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

เชี่ยวชาญ git rm: คู่มือสมบูรณ์สำหรับลบไฟล์ออกจาก Repository

คำสั่ง git rm ลบ File ออกจาก Git Repository คำสั่งนี้ลบ File จาก File System ของคุณ แล้วลบออกจาก List ของ Files ที่ Git Repository Track Flag --cached ให้ลบ File จาก Git Repository โดยไม่ลบใน File System

ลบ File จาก Git Repository ยังไง? คำถามดี ไม่ได้ต้องการให้ File เป็นส่วนของ Repository ตลอดกาลเสมอไป อาจตัดสินใจว่า File ไหนไม่จำเป็นต่อโปรเจกต์แล้ว จึงอยากลบออกจาก Git Repository

เพื่อลบ File จาก Git Repository ใช้คำสั่ง git rm เป็นตรงข้ามของ git add Command มันลบ Files

ในคู่มือนี้จะอธิบายใช้ git rm Command พร้อม Example แสดงการทำงาน เริ่มกันเลย!

Files จัดการใน Git ยังไง?

ก่อนอธิบายใช้ git rm Command ต้องเข้าใจ Files จัดการใน Git Version Control System ยังไง

Files ภายใน Git Repository ไม่ว่าจะ Tracked หรือ Untracked

Tracked Files คือ Files ที่ Included ใน Git Commit Untracked Files คือ Files ที่ยังไม่ Committed ไป Repository ปกติ Untracked Files คือ Files ที่สร้างก่อน Create Commit

เพื่อ Add File ไป Git Commit ใช้คำสั่ง git add ทำให้กลายเป็น Tracked File หากอยากหยุด Track File ใช้คำสั่ง git rm

คำสั่ง git rm ลบ File หรือ Group of Files จาก Git Repository File ถูกลบจาก Machine ของคุณและ Git Repository เพื่อ Preserve File บน Local Machine ใช้ Flag --cached

โดยไม่มี Flags คำสั่งนี้ลบ File จาก Git Repository และ Local Working Directory ทั้งคู่ หมายถึง Delete File เหมือน Linux rm Command ลบ File จาก Computer

การใช้งานง่ายที่สุดคือลบ File ลองลบ File ชื่อ settings.json จาก Git Repository:

คำสั่งนี้ลบ settings.json จาก Tracking Area ของ Repository ใช้คำสั่งเดียวกันลบ Multiple Files ได้ แยกชื่อ Files หรือ Folders ที่อยากลบด้วย Space:

git rm settings.json .env

นี่จะลบ settings.json และ .env Files จาก Repository

โดย Default Git จะ Safety Check ตอนรัน rm Command เพื่อ Unstage และ Remove Paths ทำให้แน่ใจว่า Files บน Current Branch เหมือนใน Staging Index Override Behavior นี้ได้ด้วย Flag -f หรือ --force:

ควรใช้ -f Flag เฉพาะตอนแน่ใจว่าเอาจริงๆ อยากลบ File ไม่เช่นนั้น Merge Conflict อาจเกิดภายหลัง หากเกิด ใช้ Guide Resolve Merge Conflict ช่วยได้

Git rm --cached

Flag Git rm --cached ลบ File จาก Staging Area Files จาก Working Directory จะคงอยู่สมบูรณ์ หมายถึงยังมี Copy ของ File Locally File จะถูกลบจาก Index ที่ Track Git Project

ลบ settings.json Working Tree File จาก Repository แต่เก็บไว้ใน Project Directory:

git rm --cached settings.json

ตอน Push Next Commit settings.json File จะถูกลบ

ตราบใด File อยู่ใน Local Working Directory คุณ Add กลับไป Git Repository ได้ ใช้คำสั่ง git add Command อ่านเพิ่มเติม git add ใน Tutorial git add Command

วิธี Undo git rm Command

คำสั่ง git rm เพียง Update Staging Area และ Working Directory จนกว่าจะ Commit หมายถึง Revert Command ได้ เพื่อ Undo git rm Command ให้ Reset Repository ไป Last Commit ด้วย git reset:

Git HEAD Commit แทน Last Commit ใน Repository ดังนั้นคำสั่งนี้ Revert Repository ไป Last Commit

Revert git rm Command ได้แม้ Committed Changes ไป Repository แล้ว ใช้ git reset Command ระบุ Hash ของ Commit ที่อยาก Revert:

git reset a7e3cce9637c74281e6590003b39d3990bbb2731

เรา Revert Git Repository ไป Previous Commit String ตัวอักษรหลัง "reset" คือ Hash สำหรับ Last Commit

หลัง Revert Repository เรา View ว่า Repository ลักษณะอย่างไรก่อน Commit หมายถึง File ยัง Staged อยู่ กล่าวคือ git rm Command ของเราถูก Undo แล้ว

Git rm vs. rm

คำสั่ง git rm ลบ File จาก Working Machine และ Git Repository ทั้งคู่ คำสั่ง rm ไปทางอื่น ไม่ลบ File จาก Git Repository

หากอยากลบ File จาก Git Repository ต้องใช้ git rm เพราะ git rm Execute Instructions ลบ File จาก Git Repository ไม่ใช่ทุก Files บน Linux System อยู่ใน Git Repository ดังนั้น Linux rm Command ไม่ลบ Files จาก Git Repository

สรุป

คำสั่ง git rm ให้ลบ File จาก Git Repository และ Working Directory หากอยากลบจาก Git Repository เท่านั้น ใช้ Flag --cached

เชี่ยวชาญ git rm: คู่มือสมบูรณ์สำหรับลบไฟล์ออกจาก Repository

"Career Karma เข้ามาในชีวิตฉันเมื่อฉันต้องการมากที่สุด และช่วยจับคู่กับบูทแคมป์ได้เร็ว สองเดือนหลังจบ ฉันพบงานฝันที่ตรงกับคุณค่าและเป้าหมายในชีวิต!"

Venus, วิศวกรซอฟต์แวร์ที่ Rockbot

ค้นหาบูทแคมป์ของคุณ

ตอนนี้คุณพร้อมใช้คำสั่ง git rm อย่าง Command Line Expert แล้ว!

อ่านเพิ่มเติมการทำงานกับ Git ใน How to Learn Git Guide