ขณะทำงานกับ Git คุณจะมีหลาย instances ที่หลังเพิ่มบรรทัดโค้ดใหม่ใน .gitignore file ไฟล์ที่ ignored ยังแสดงใน "git commit" staging area เมื่อเจอกรณีเหล่านี้ วิธีแก้ที่ดีที่สุดคือ clear และ clean Git cache คู่มือนี้แสดงวิธี clear Git cache แต่ก่อนจะลงลึก มาพูดสั้นๆ เกี่ยวกับ Git cache คืออะไร ทำอะไร และข้อดีของการ clear มัน
Git Cache คืออะไร?
Git cache เรียกอีกอย่างว่า staging area หรือ index ประกอบด้วย working tree directory รวมถึง repository, commits, และ branches ที่จะ committed ทันทีที่คุณเรียกคำสั่ง "git commit" ณ เวลาใดก็ตาม
Cache ช่วยให้คุณทำ selected changes ไปยัง working tree ก่อน commit หรือ download only most recent commits ขณะ cache ส่วนใหญ่อื่นๆ
โดยไม่มี Git cache git commit จะ revert commit changes ไปยัง working tree อย่างไม่สะดวก ก่อน commit บางส่วนของ commit changes ใน next commit
Git Cache ทำอะไร?
จิตใจของ staging area/index คือเพิ่ม performance โดย resolve conflicting commit merges และ minimize consistent need to redownload dependencies, libraries, และ content-types อื่นๆ on the fly ทุกครั้งที่มีความจำเป็น
Git Cache อยู่ที่ไหน (และหายังไง)
Git cache อยู่ในไฟล์ชื่อ index ภายใน directory .git
เพื่อหา หรือไปยัง Git cache file รันคำสั่งต่อไปนี้เพื่อเข้า .git directory หลังได้ location ของ .git directory navigate เข้าไปด้วย cd:
git rev-parse --git-dir cd .git
อยู่ใน directory แล้ว locate index file ด้วย ls:
ls –la
ดูไฟล์ด้วยคำสั่ง file:
file index
คำสั่งจะให้ output แสดง file type, version, และจำนวน entries ภายใน Git cache file
index: Git index, version 2, 5 entries
Clear Git Cache File
เพื่อลบไฟล์เฉพาะจาก Git cache ใช้คำสั่ง git rm ตามด้วย specific file
ลบไฟล์จาก cache แบบ recursive ใช้ flag -r กับ git rm
Syntax ทั่วไป:
git rm --cached filename
แทนที่ filename ด้วยไฟล์เฉพาะที่ต้องการลบจาก Git cache
ตัวอย่าง ลบไฟล์ "mte-info.c":
git rm --cached mte-info.c
ตรวจสอบว่าไฟล์ถูกลบสำเร็จด้วยคำสั่ง:
file .git/index
หมายเหตุ: การรัน git rm --cached filename ไม่ได้ลบไฟล์จาก working directory – แค่จาก staging area
อีกทางหนึ่ง หากต้องการ clear entire cache และ staging area ใช้ git rm ด้วย recursive -r option:
git rm -r –cached
สรุป
จาก tutorial นี้ คุณเรียนรู้ว่า clear Git cache ทำได้ง่าย ขณะเดียวกัน ควรเรียนรู้ Git alias เพื่อทำให้ Git usage มีประสิทธิภาพมากขึ้น