Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> ระบบ >> Linux

คู่มือ Git สำหรับมือใหม่ (The Beginner's Guide to Git)

หากคุณเป็น Linux user น่าจะเคยเจอ Git บ้าง อาจขณะพยายาม download โปรแกรมใหม่ หรือดู version control systems อย่าง CVS หรือ Subversion Git คือ revision control system ที่ Linus Torvalds สร้างขึ้น เนื่องจากไม่พอใจกับ existing solutions จุดเน้นหลักใน design คือ speed หรือ具体来说 efficiency Git แก้ไข shortcomings หลายอย่างของระบบก่อนหน้า และทำทั้งหมดในเวลาน้อยกว่ามาก หากอยากเรียนรู้ Git คู่มือมือใหม่จะช่วยเริ่มต้นได้

Git ทำอะไร?

สมมติคุณกำลังสร้าง website ให้ลูกค้า พวกเขาบอกว่าอยากได้อะไร คุณ design พวกเขา review และทำ revisions ฯลฯ ด้วยแต่ละชุด revisions จากลูกค้า site เปลี่ยนและโตขึ้น ต่อมา ลูกค้าอาจพูดว่า "ชอบแบบเดิมตอนกันยายนที่แล้วดีกว่า" ภายใต้ normal circumstances คุณมีปัญหา อาจไม่มีไฟล์และ data ทั้งหมดจากเวลานั้น และ code อาจเปลี่ยนมากจน revert ยากหรือ impossible

จุดของ revision control system คือแก้ nearly all problems ในย่อหน้าข้างต้น คุณสามารถ track แต่ละการเปลี่ยนแปลงของ code และ files และ revert กลับไปที่จุดใดก็ได้

Git ทำงานอย่างไร?

Directory ของแต่ละ project เป็น Git repository ของตัวมันเอง เก็บไฟล์ทั้งหมดของ project นั้นใน directory นั้น และเป็นระยะบอก Git update information ด้วย current state ของไฟล들 กระบวนการบอก Git record state นี้คือ commit ทุกครั้งที่ commit (ควรบ่อย) Git ดูที่ไฟล์ทั้งหมดที่ถูกสั่ง track และ save differences ในไฟล์เหล่านั้น (ไม่ใช่ all new files) ไปที่ directory ".git" แต่ละ commit กลายเป็น new save point สำหรับ development ของ project

ทางเลือกคุณสามารถ push local git repository ไป outside host เช่น GitHub หรือ Git server ของตัวเอง นี่อนุญาตให้ multiple contributors บน project ทำ frequent, fast commits ไป local repo แล้ว bundle all those local commits เป็น single update ไป online repository

นี่คือหนึ่งในสิ่งที่ทำให้ Git เร็วกว่า Version Control Systems (VCS) บางตัว: commit บ่อยไป local repository โดยไม่ waste time และ bandwidth upload ไป server ทุกครั้ง

การได้ Git (Getting Git)

Linux distros ส่วนใหญ่มาพร้อม Git preinstalled หากระบบไม่มาพร้อม ติดตั้งได้ง่ายจาก Software Center หรือ package manager เพราะ available ใน repositories ส่วนใหญ่

ติดตั้งใน terminal:

# Debian/Ubuntu
sudo apt install git-all
# Fedora
sudo dnf install git-all

หากอยู่ platform อื่นหรือไม่สามารถใช้ repositories ได้ ดาวน์โหลดและติดตั้ง packages แบบ manual ได้ที่นี่

การใช้ Git (Using Git)

กระบวนการสร้าง local git repository เร็วและง่าย แรก สร้าง directory ที่ตั้งใจใช้สำหรับ project และเปิด terminal ที่ location นั้น

cd /my-git-directory

เริ่ม Git repo ด้วยคำสั่ง:

git init

จะสร้าง directory ".git" (hidden from view) เก็บ repository information น่าจะอยากเริ่มเพิ่มไฟล์บ้าง เริ่มด้วยการสร้าง README file เรียบง่าย เพิ่มเข้า repository's list of files to watch แล้ว commit file ไป repository

# ใส่ข้อความบางอย่างลงไฟล์ใหม่
echo "TODO: Create documentation" > README.txt
# บอก Git track การเปลี่ยนแปลงของไฟล์นี้
# ต้องทำเพียงครั้งเดียวต่อไฟล์ (รายละเอียดเพิ่มเติมในภายหลัง)
git add README.txt
# และตอนนี้ save state ไป Git repository
git commit README.txt

จะถูกนำไป text editor screen (ขึ้นอยู่กับ distro และ configuration) ใส่ notes เกี่ยวกับ commit นี้ สรุปสั้นๆ ของการเปลี่ยนแปลงที่เกิดขึ้นตั้งแต่ last commit save และ exit text editor commit ควรสำเร็จ

หาก Git throw error ว่าไม่ recognize คุณ ต้องบอกมันว่าคุณคือใคร ทำได้ง่ายดายด้วย:

git config --global user.name "Your_Username"
git config --global user.email "your_email_address@mailserver.com"

เราสร้าง snapshot ของ current state ของไฟล์นั้นแล้ว การเปลี่ยนแปลงเพิ่มเติม (ที่ commit) จะ save บน top ของนั้น

Add และ commit แต่ละไฟล์แยกกันเหมือนตัวอย่างข้างต้นอาจ tedious เพื่อแก้ ใช้ add all files ใน current directory ด้วย

# สังเกต trailing "."
git add .

Commit all known changed files ทีเดียวด้วย

git commit -a

Git command options อื่นๆ ที่ handy:

# ทำ full clone ของ existing repository เช่นจาก website ของ software project
git clone (URL, ie git://github.com/github/linux-2.6.git)
# Move/rename ไฟล์ ช่วยให้ไม่ต้อง remove และ re-add file หากมันถูก move โดย bash
git mv (source) (destination)
# Delete ไฟล์และ remove จาก Git repo
git rm (target)
# ดู branches ใน repository นี้
git branch
# สร้าง branch ใหม่ของ Git tree
git branch (new branch name, ie "experimental")
# Switch จาก branch หนึ่งไปอีก branch
git checkout (branch name, ie "experimental")
# Merge branch (branch) เข้า current tree
git merge (branch)

นั่นแค่ต้นเรื่องของสิ่งที่ Git ทำได้ เมื่อ mastered Git แล้ว เริ่มกับ Github และเรียนรู้เพิ่ม screenshots และ animation 到 pull requests