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

วิธีตั้งค่า Git ด้วย git config

วิธีตั้งค่า Git ด้วย git config

คำสั่ง git config เปลี่ยน Configuration Options ใน Git Installation ของคุณ มักใช้ตั้งค่า Git Email, Editor, และ Aliases ที่ต้องการใช้กับคำสั่ง git

Git เป็น Distributed Version Control System ยอดนิยมที่สุดในโลก ทุก Developer ที่ทำงานโปรเจกต์ Git จะมี Copy ของ Repository อยู่ Local หมายความว่าหลายคนสามารถร่วมงานโปรเจกต์เดียวกันพร้อมกันได้

ตอนเริ่มต้น Git มี Setup บางอย่างที่ต้องทำ Setup นี้ต้องทำแค่ครั้งเดียวบนคอมพิวเตอร์ของคุณ

ในคู่มือนี้ เราจะพูดถึงวิธี Setup Git ด้วยคำสั่ง git config จะเดินทางผ่านคำสั่ง git config หลายคำสั่งที่ใช้เริ่มต้นได้ เริ่มเลย!

คำสั่ง git config

คำสั่ง git config ตั้งค่า Configuration Values สำหรับ Git Installation ของคุณ ก่อนเริ่มใช้ Git สำหรับโปรเจกต์ คุณจะใช้คำสั่งนี้ Config Git Name และ Email บนคอมพิวเตอร์

คำสั่งนี้แก้ไขเนื้อหาของ Git Config Files ไฟล์เหล่านี้เก็บข้อมูล เช่น Username, Default Editor, และ Email ที่ต้องการเชื่อมโยงกับ Commits

ก่อนเริ่มทำงานกับ Repositories คุณต้องทำ Initial Configuration เราจะครอบคลุม 5 หัวข้อ:

  • Configuration Levels
  • การสร้าง Identity
  • การตั้งค่า Editor
  • การสร้าง Alias
  • การ View และ Reconfigure Git

Git Config: Configuration Levels

ก่อนเริ่ม เราต้องพูดถึง Configuration Levels ต่างๆ สำหรับ Default Git Configuration Options

Configuration Values สามารถตั้งได้ที่ 3 ระดับ:

  • --local: Local Values จะ Apply กับ Repository ที่รัน git config Command ค่าเหล่านี้เก็บใน .git/config ภายใน Repository
  • --system: System Values Apply กับทุก User บนเครื่อง ควรตั้ง System-Level Config ด้วยความระมัดระวังเพราะอาจเปลี่ยน Existing Configurations ค่าเก็บใน /etc/gitconfig บน Linux
  • --global: Global Values Apply กับ User เฉพาะบน OS เก็บอยู่ใน ~/.gitconfig ใน Home Directory ของคุณ

ตอน Setup Git ครั้งแรก คุณจะใช้ --global Level เป็นหลัก

Git Config Username Command

เพื่อตั้งค่า Git Username ให้รัน git config --global user.name ควรระบุ First และ Last Name แต่อย่างใดก็ได้ที่ต้องการ Attach กับ Commits

Git Username ไม่จำเป็นต้องเหมือน Version Control Username เช่นบน GitHub

คุณต้อง Setup Identity เมื่อ Install Git ครั้งแรก นี่เป็น Mandatory เพราะทุก Commit มี Name และ Email Address คุณไม่สามารถเปลี่ยน Authorship Information ของ Commit ได้หลังจากสร้างแล้ว

มีข้อมูล 2 อย่างที่ต้องระบุ: Name และ Email

ลอง Config Username Values ด้วย git config:

git config --global user.name "Sarah Smith"

นี่จะตั้ง User Name เป็น Sarah Smith Future Commits ทั้งหมดจะอ้างอิงข้อมูลนี้ เราใช้ Option --global เพื่อ Apply Default Git Config นี้กับทุก Repositories ที่ User ของเราเป็นเจ้าของ

Git Config Email Command

เพื่อ Config Git Email Address ให้รัน git config --global user.email คำสั่งนี้รับ 1 Argument: Email Address ของคุณ

git config --global user.email "sarah.smith@email.com"

เราสามารถดู Configuration Values ที่ตั้งได้โดยเช็ค Global Config File (~/.gitconfig):

[user] email = sarah.smith@email.com name = Sarah Smith

Identity ของเราถูก Configure สำเร็จแล้ว!

Git Config Editor Command

คุณชอบ Vim? เป็นแฟน Emacs? หรือ Nano ตอบโจทย์ทุกอย่าง? Text Editor 任何您喜欢的,最好告诉 Git 因为有许多命令(如 git commit)会打开一个文本编辑器供您输入。

ลองตั้ง Nano เป็น Default Code Editor:

git config --global core.editor "nano"

ทุกครั้งที่เรารันคำสั่งที่เปิด Text Editor จะใช้ Nano คุณสามารถแทน Nano ด้วย Text Editor 任何已安装在您的系统上。

ทุกครั้งที่เรารันคำสั่งที่เปิด Text Editor จะใช้ Nano คุณสามารถแทน Nano ด้วย Text Editor 任何已安装在您的系统上。

วิธีสร้าง Alias

เบื่อพิมพ์คำสั่งเดิมๆ ซ้ำๆ? Git Aliases ช่วยคุณได้ พวกเขาให้คุณเขียน Shortcuts สำหรับคำสั่งทั่วไปที่คุณพิมพ์

อยากเขียน git co แทน git commit มีคำสั่งยาวอยากย่อ? เราสามารถเขียน Git Aliases สำหรับทุกกรณีได้

ลองเขียน Git Alias เรียกคำสั่ง git commit:

git config --global alias.co commit

ทุกครั้งที่เรารัน git co คำสั่ง git commit จะถูก Run

วิธี View Git Configuration File

คุณสามารถ View Individual Configuration Value ได้ด้วย git config ตามด้วย Value ที่ต้องการ View:

git config user.name

คืนค่า "Sarah Smith" นี่คือ Value ที่เราตั้งไว้ก่อนหน้า

คุณสามารถใช้ git config --list ดู Configuration Values ทั้งหมดที่เชื่อมโยงกับ Git Installation ของคุณ:

git config --list

ตัวอย่างสิ่งที่คุณอาจเห็น:

user.email=sarah.smith@email.com user.name=Sarah Smith filter.lfs.clean=git-lfs clean -- %f filter.lfs.smudge=git-lfs smudge -- %f …

สรุป

คำสั่ง git config Config Git Installation ของคุณ

ตอนเริ่มใช้ Git ควร Config Name และ Email เพื่อให้ Git รู้ Identity ที่ต้อง Attach กับ Commits

คุณควรตั้ง Editor ด้วย เพื่อให้คำสั่งที่อ้างอิง Text Editor จะใช้ Preferred Text Editor ของคุณเปิดไฟล์

สำหรับ Learning Resources และ Tutorials เพิ่มเติมเกี่ยวกับวิธีเรียนรู้ Git ดูคู่มือ How to Learn Git ของเรา