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

Git Clone: คำสั่งโคลน Repository

คำสั่ง git clone สร้าง copy ของ remote repository บน local machine โดย default clone command save โค้ดในโฟลเดอร์ชื่อเดียว repository สามารถ override ได้โดยระบุ folder name หลัง URL ของ repository ที่ต้องการ clone

การสร้าง local copies ของ Git repository ที่เก็บที่อื่นเป็นส่วนสำคัญของ Git version control system

คำสั่ง git clone ให้สร้าง local copy ของ repository ที่เก็บที่อื่น copy นี้เรียกว่า "clone"

บทช่วยสอนนี้สำรวจพื้นฐาน git clone command การ clone local และ remote repository และการ clone bare repository

Git Clone Command คืออะไร?

คำสั่ง git clone สร้าง copy ของ existing repository ไป working directory บน local computer

Cloning เป็นฟีเจอร์สำคัญของ Git เพราะ cloning ให้สร้าง copy ของโค้ดอิสระจาก main version สามารถ run และ manipulate โค้ดบน local machine โดยไม่ affect โค้ดใน main version

หมายถึงไม่ต้องทำการเปลี่ยนแปลง repository จนกว่าจะพร้อม หลังทำการเปลี่ยนแปลง สามารถ push โค้ดไป remote repository เพื่อเก็บ หรือสร้าง pull request ให้คนอื่น review โค้ดที่เขียน

มี 2 วิธี setup repository ด้วย Git: clone existing repository ด้วย git clone หรือใช้ git init สร้าง repository ใหม่

Cloning repository มักเป็น one-time action หลัง clone แล้ว จะมีโค้ดทั้งหมดบน local machine พร้อมทำงานกับ Git repository

git clone command clone metadata ทั้งหมดของ repository หลัง clone จะมี record ของประวัติโปรเจกต์ทั้งหมด clone command ยัง clone Git branches ทั้งหมดของโปรเจกต์

วิธีใช้ Git Clone

git clone สร้าง clone ของ existing repository ใน directory ใหม่บน local machine เมื่อ clone repository ไป local machine จะสร้าง environment ใหม่เก็บโค้ด repo

ตัวอย่าง clone repository จาก GitHub ไป local machine:

git clone https://github.com/username/project-name.git
cd project-name

คำสั่งแรกสร้าง Git repository ใหม่จาก GitHub URL ที่ระบุ ชื่อโฟลเดอร์ที่โค้ดปรากฏ = ชื่อ repository ในกรณีนี้ Git สร้างโฟลเดอร์ project-name

คำสั่ง cd ย้ายเข้า repository ใหม่ ตอนนี้ view repository ใหม่ สามารถ view และ edit ไฟล์ได้

Custom Clone Operations

git clone รับ parameters สำหรับ custom clone operations สำรวจตัวอย่างหลัก:

Clone ไปโฟลเดอร์เฉพาะ (Clone to a Folder)

Default สร้างโฟลเดอร์ชื่อเดียว repository ตัวอย่างแรก clone โดยไม่ระบุ folder name resultant copy เรียกว่า ck-git เพราะไม่ได้ระบุ folder name

แต่เวลา clone อาจอยาก clone ไป specific folder ระบุ directory name ใน git clone command:

git clone <repo> <folder>

สมมติ clone hypothetical Git repo ไปโฟลเดอร์ "my-project":

git clone https://github.com/username/project-name.git my-project
cd my-project

โค้ด clone GitHub repo ที่อ้างอิง ระบุ folder name "my-project" ที่โค้ดจะถูก clone แล้ว cd เข้า my-project folder ที่ git clone สร้าง

สร้าง Shallow Clone (Create a Shallow Clone)

Default clone repository จะ clone history ด้วย หากโปรเจกต์มีหลายร้อย commits จะใช้เวลานานสร้าง local clone

ข้ามการ clone ทั้ง repository ด้วย shallow clone Shallow clones clone แค่ history ของ commits ตาม "depth" parameter

ไวยากรณ์:

git clone -depth=1 <repo>

ค่า "depth" = จำนวน historical commits ที่ต้องการ clone ตัวอย่างระบุ 1 = clone แค่ most recent commit

สมมติ hypothetical GitHub repo มี 10,000 commits clone ใช้เวลานาน ต้องการแค่ current code และ 10 last commits โดยไม่ clone ทั้ง history ใช้:

git clone -depth=10 https://github.com/username/project-name.git

คำสั่งสร้าง copy โปรเจกต์สถานะปัจจุบัน สามารถย้อนกลับ 10 commits ในประวัติ แต่ไม่มี data commits ก่อนหน้านั้น

อ่าน official Git documentation เกี่ยว git clone สำหรับ advanced configuration options

Git URLs

มี URL types หลายประเภทที่ git clone รองรับ git clone มัก clone remote repositories สำรวจ URL types หลัก:

Git Protocol

Git protocol ใช้ clone repository ด้วย Git version control system ไม่ใช้ authentication ตัวอย่าง URL:

git://host.com/repo/path.git

URL เริ่ม "git:" บอก Git client clone ด้วย git protocol

SSH

Secure Shell (SSH) เข้าถึง networked servers remotely SSH ให้ authentication SSH URLs มักใช้ secure Git repositories ตัวอย่าง:

ssh://user@host.com/repo/path.git

"user" = ชื่อ user ที่เข้าถึง repository ต้อง sign in ผ่าน SSH ก่อน clone

HTTP/HTTPS

HyperText Transfer Protocol (HTTP) ใช้ transfer web page data ข้าม internet HTTP URLs ใช้ Git repositories ได้ ตัวอย่าง:

https://host.com/repo/path.git

ตัวอย่างใช้ HTTPS protocol แทน "git" หรือ "ssh" ใช้ "https" นำหน้า Git repository URL

สรุป

คำสั่ง git clone ใช้สร้าง local copy ของ Git repository Cloning ให้สร้าง independent copy ของ repository ที่ edit ได้โดยไม่ affect main version ของโปรเจกต์

เมื่อพร้อมบันทึกการเปลี่ยนแปลงไป main version สร้าง commit

บทช่วยสอนสำรวจการใช้ git clone พร้อมตัวอย่าง ตอนนี้คุณมีความรู้ clone repositories ด้วย git clone อย่างมืออาชีพแล้ว!