Gitกวดวิชา


Gitและ {{title}}


Git Contribute


Git Advanced


Git Undo




Git Ignore และ .gitignore


Git ละเว้น

เมื่อแบ่งปันรหัสของคุณกับผู้อื่น มักจะมีไฟล์หรือบางส่วนของโครงการที่คุณไม่ต้องการแบ่งปัน

ตัวอย่าง

  • ไฟล์บันทึก
  • ไฟล์ชั่วคราว
  • ไฟล์ที่ซ่อนอยู่
  • ไฟล์ส่วนตัว
  • เป็นต้น

Git สามารถระบุไฟล์หรือบางส่วนของโครงการของคุณที่ควรละเว้นโดย Git โดยใช้.gitignoreไฟล์

Git จะไม่ติดตามไฟล์และโฟลเดอร์ที่ระบุใน.gitignore. อย่างไรก็ตาม.gitignore ไฟล์นั้นถูกติดตามโดยGit


สร้าง .gitignore

ในการสร้าง.gitignoreไฟล์ ให้ไปที่รูทของ Git ในเครื่องของคุณ และสร้างมันขึ้นมา:

ตัวอย่าง

touch .gitignore

ตอนนี้เปิดไฟล์โดยใช้โปรแกรมแก้ไขข้อความ

เราจะเพิ่มกฎง่ายๆสองข้อ:

  • ละเว้นไฟล์ใด ๆ ที่มี.logนามสกุล
  • ละเว้นทุกอย่างในไดเร็กทอรีใด ๆ ที่ชื่อ temp

ตัวอย่าง

# ignore ALL .log files
*.log

# ignore ALL files in ANY directory named temp
temp/

ตอนนี้.logไฟล์ทั้งหมดและทุกอย่างใน tempโฟลเดอร์จะถูกละเว้นโดย Git

หมายเหตุ:ในกรณีนี้ เราใช้รายการเดียว.gitignoreที่ใช้กับที่เก็บทั้งหมด

เป็นไปได้ที่จะมี.gitignoreไฟล์เพิ่มเติมในไดเร็กทอรีย่อย สิ่งเหล่านี้ใช้กับไฟล์หรือโฟลเดอร์ภายในไดเร็กทอรีนั้นเท่านั้น



กฎสำหรับ .gitignore

กฎทั่วไปสำหรับรูปแบบการจับคู่ใน.gitignore ไฟล์มีดังนี้ 

Pattern Explanation/Matches Examples
  Blank lines are ignored  
# text comment Lines starting with # are ignored  
name All name files, name folders, and files and folders in any name folder /name.log
/name/file.txt
/lib/name.log
name/ Ending with / specifies the pattern is for a folder. Matches all files and folders in any name folder /name/file.txt
/name/log/name.log

no match:
/name.log
name.file All files with the name.file /name.file
/lib/name.file
/name.file Starting with / specifies the pattern matches only files in the root folder /name.file

no match:
/lib/name.file
lib/name.file Patterns specifiing files in specific folders are always realative to root (even if you do not start with / ) /lib/name.file

no match:
name.file
/test/lib/name.file
**/lib/name.file Starting with ** before / specifies that it matches any folder in the repository. Not just on root. /lib/name.file
/test/lib/name.file
**/name All name folders, and files and folders in any name folder /name/log.file
/lib/name/log.file
/name/lib/log.file
/lib/**/name All name folders, and files and folders in any name folder within the lib folder. /lib/name/log.file
/lib/test/name/log.file
/lib/test/ver1/name/log.file

no match:
/name/log.file
*.file All files withe .file extention /name.file
/lib/name.file
*name/ All folders ending with name /lastname/log.file
/firstname/log.file
name?.file ? matches a single non-specific character /names.file
/name1.file

no match:
/names1.file
name[a-z].file [range] matches a single character in the specified range (in this case a character in the range of a-z, and also be numberic.) /names.file
/nameb.file

no match:
/name1.file
name[abc].file [set] matches a single character in the specified set of characters (in this case either a, b, or c) /namea.file
/nameb.file

no match:
/names.file
name[!abc].file [!set] matches a single character, except the ones spesified in the set of characters (in this case a, b, or c) /names.file
/namex.file

no match:
/namesb.file
*.file All files withe .file extention /name.file
/lib/name.file
name/
!name/secret.log
! specifies a negation or exception. Matches all files and folders in any name folder, except name/secret.log /name/file.txt
/name/log/name.log

no match:
/name/secret.log
*.file
!name.file
! specifies a negation or exception. All files withe .file extention, except name.file /log.file
/lastname.file

no match:
/name.file
*.file
!name/*.file
junk.*
Adding new patterns after a negation will re-ignore a previous negated file
All files withe .file extention, except the ones in name folder. Unless the file name is junk
/log.file
/name/log.file

no match:
/name/junk.file

กฎละเว้น Git ในพื้นที่และส่วนบุคคล

นอกจากนี้ยังสามารถละเว้นไฟล์หรือโฟลเดอร์ต่างๆ ได้ แต่ไม่แสดงใน.gitignoreไฟล์ที่แยกย่อย

การละเว้นประเภทนี้ระบุไว้ใน .git/info/excludeไฟล์ มันทำงานในลักษณะเดียวกับ .gitignoreแต่จะไม่แสดงให้ใครเห็น


ทดสอบตัวเองด้วยแบบฝึกหัด

ออกกำลังกาย:

ใน.gitignoreการเพิ่มบรรทัดเพื่อละเว้น.tempไฟล์ทั้งหมด: