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

คู่มือ Git Add แบบ Step-by-Step

คำสั่ง git add เพิ่มไฟล์หรือโฟลเดอร์เข้า Staging Area ไฟล์ใน Staging Area คือไฟล์ที่คุณต้องการเพิ่มเข้า Next Commit Git add ไม่ Modify หรือส่งผลกระทบ Repository หรือ Files โดยตรง


Git Version Control System ไม่มี Simple Save Feature Git ใช้ Process เรียกว่า Committing นี่คือที่ที่คุณสร้าง Record ของการเปลี่ยนแปลงทั้งหมดที่ทำไปที่ Files นับจาก Last Commit

Process นี้เกี่ยวข้องกับ Command Line Operations หลายอย่าง ขั้นตอนแรกในการ Save File/Folder Changes เข้า Git Repository คือใช้คำสั่ง git add คำสั่งนี้ให้คุณเลือกไฟล์ที่ต้องการ Commit เข้า Repository โดยย้าย Directory เข้า Staging Area

บทช่วยสอนนี้จะอภิปรายพร้อมตัวอย่างวิธีใช้ git add เลือกไฟล์ที่ต้องการ Commit เข้า Repository จบบทความคุณจะเป็น Expert ในการใช้ git add แล้ว

Saving Files in Git: การบันทึกไฟล์ใน Git

เวลา Save File บน Computer Computer จะ Store Changes บน Local Machine เช่น Save Word Document Computer จะ Store New Version ของ Document

Git ใช้ Approach ต่างกันสำหรับ Save Changes เวลา Save File ในโปรเจกต์ที่ทำงาน Changes จะ Saved บน Computer แต่เพื่อให้ Git Repository สะท้อน Changes คุณต้อง Commit Changes เหล่านั้นเข้า Repository Flowchart ด้านล่างสะท้อนวิธีทำ:

คู่มือ Git Add แบบ Step-by-Step

หลังจากเปลี่ยน File ที่ Retrieve จาก Git Repository Repository เองจะไม่เปลี่ยนแปลง เว้นแต่คุณผ่าน Steps นี้ แม้ Computer จะ Store New Version ของ File Main Repository—ซึ่งมักเก็บบน Server อื่น—จะไม่เปลี่ยน นอกจากนี้ Git จะไม่ Track การเปลี่ยนแปลงที่คุณทำไปที่ File จนกว่าคุณ Commit Updated File(s) เข้า Repository

Workflow สำหรับเปลี่ยน File/Folder ใน Git Repository 长这样:

  1. บน Computer ให้ Update File/Folder ใน Git Repository
  2. ใช้ git add เพิ่มการเปลี่ยนแปลงเหล่านั้นเข้า Staging Area
  3. ใช้ git commit ย้าย Changes จาก Staging Area เข้า Commit
  4. ใช้ git push Push การเปลี่ยนแปลงเหล่านั้นไป Main Repository

เพื่อ Save File ใน Git คุณต้องใช้ git add และ git commit commands ในบทช่วยสอนนี้ เราอภิปรายคำสั่งแรก—คือ git add command

git add Command คืออะไร?

คำสั่ง git add เพิ่มไฟล์เข้า Git Staging Area Area นี้มี List ของไฟล์ทั้งหมดที่คุณเปลี่ยนแปลงล่าสุด Repository ของคุณจะ Update ครั้งต่อไปที่คุณสร้าง Commit ด้วยการเปลี่ยนแปลงของคุณ

ดังนั้น การรัน git add command ไม่เปลี่ยน Work ใดๆ ใน Git Repository ของคุณ Changes ทำได้ก็ต่อเมื่อ Execute git commit command

Syntax สำหรับ git add command:

git add fileName

File ที่ระบุอาจเป็น File หรือ Folder 任何在您的 Git Repository 中。

สมมติเราต้องการเพิ่มไฟล์ชื่อ README.md เข้า Staging Area เพราะต้องการ Commit เข้า Main Repository ทำได้ด้วย:

git add README.md

คำสั่งนี้เพิ่มไฟล์ README.md เข้า Staging Area

มีวิธี Apply คำสั่งด้านบนกับ Updated Files และ Folders ทั้งหมดจาก Repository Code สำหรับนี้คือ:

git add --all

คำสั่งนี้เพิ่มทุกการเปลี่ยนแปลงที่เราทำไปที่ Files และ Folders จาก Repository เข้า Git Staging Area

ตอนที่เรา Add Files เข้า Staging Area แล้ว เราสามารถ Commit พวกเขาเข้า Repository ด้วย git commit command เราอภิปราย git commit command ในคู่มือ git commit ของเรา

Git Add All Files

คุณสามารถ Add Files ทั้งหมดใน Repository เข้า Staging Area ได้ด้วย git add -A หรือ git add .

Flag -A บ่งชี้ว่าต้องการ Add All Files เข้า Repository Flag นี้เป็น Shorthand สำหรับ --all คุณสามารถใช้ --all แทนได้ หากชอบ ลอง Add การเปลี่ยนแปลงทั้งหมดที่เราทำเข้า Staging Area:

git add -A

Staging Area ตอนนี้มีการเปลี่ยนแปลงทั้งหมดที่เราทำไปที่ Files

เรายังใช้ git add . command ได้ คำสั่งนี้เพิ่ม Files ทั้งหมดใน Folder ที่คุณกำลัง View อยู่เข้า Staging Area

เพื่อ Add All Files เข้า Staging Area ด้วย git add . คุณต้อง View Root Folder ในโปรเจกต์ นี่คือ Main Folder ที่เก็บ Code ทั้งหมดของคุณ

ลองใช้ git add . command:

git add .

เราเพิ่ม Files ทั้งหมดใน Repository เข้า Staging Area แล้ว

Git Staging Area

ตลอด Tutorial เราอ้างอิง Concept ของ Staging Area ใน Git Staging Area นี้เป็น Unique Feature ของ Git Version Control System มันทำหน้าที่เป็น Buffer ระหว่าง Unsaved Codebase Changes และ History ของ Project

Staging Area เป็น Feature ที่มีประโยชน์ มันให้คุณทำ Changes ไปที่ Files ใน Repository โดยไม่เปลี่ยน Repository เอง คุณ Edit Local Files และ Commit ภายหลังเข้า Repository

Use Case 潜在的一个 是 หากคุณต้องทำ Changes หลาย Files หากเป็นเช่นนั้น คุณอาจอยาก Split Changes เป็น Multiple Commits นี่จะช่วย Ensure ว่าคุณไม่ Push Changes เยอะเกินไปเข้า Repository พร้อมกัน นี่เป็น Bad Practice โดยทั่วไป เพราะทำให้ยากต่อ Track Changes ระหว่าง Commits

ด้วย git add คุณสามารถ Selectively Add ไฟล์บางไฟล์เข้า Commit ได้

Version Control คือการสร้าง Commits ที่ Simple to Track Commits ที่ Track ง่ายช่วย Developers เมื่อต้อง Identify Source ของ Coding Problems ที่เกิดขึ้น

สรุป

คำสั่ง git add เป็นคำสั่งแรกใน Series of Operations ใช้ Save Changes เข้า Git Repository Command ส่ง Changes ที่คุณทำเข้า Staging Area คุณสามารถใช้ git commit Commit Changes เหล่านั้นเข้า Main Repository

บทช่วยสอนนี้อภิปรายพร้อมตัวอย่างวิธีใช้ git add command เพิ่ม Files และ Folders เข้า Git Staging Area ตอนนี้คุณพร้อมใช้ git add command อย่าง Professional Programmer แล้ว!

เพื่อเรียนรู้ Git เพิ่มเติม อ่านคู่มือ How to Learn Git ของเรา