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

วิธีตั้งค่า Git Username และ Email ใน Ubuntu

วิธีตั้งค่า Git Username และ Email ใน Ubuntu

Git เป็น distributed version control system ที่พัฒนาโดย Linus Torvalds ช่วยสร้าง Linux kernel ตั้งแต่เริ่มต้น Git ก้าวหน้ามาก กลายเป็น version control system ยอดนิยมที่สุด

Git อนุญาตให้ multiple users contribute ไปยัง single project พร้อมกัน track changes, revert ไป previous versions, และ create branches สำหรับ project versions ต่างๆ นี่คือเหตุผลที่สำคัญต้องตั้งค่า username และ email ใน Git เพื่อให้แต่ละ commit trace back ไปยัง user ได้

จุดประสงค์ของคู่มือคือพาคุณผ่าน basics ของการเริ่มต้นกับ Git หลังติดตั้ง โดยเฉพาะการตั้งค่า username และ email ใน Git

หมายเหตุ: แม้เราใช้ Ubuntu สำหรับ tutorial นี้ ขั้นตอนจะใช้ได้ไม่ว่าคุณจะทำงานกับ OS อะไร

วิธีตั้งค่า Global Git Username และ Email

หลังติดตั้ง Git ตั้งค่า username และ email address Git อนุญาตให้ตั้ง global username และ email ใช้ในทุก git projects หรือ local credentials ใช้ใน specific repository

ตั้งค่า git credentials ใช้คำสั่ง git config Git config เป็น built-in tool ให้ view และ set git variables

ใน Ubuntu git configuration variables อยู่ใน directories:

  • /etc/gitconfig – เก็บ git configurations สำหรับ all users และ repositories
  • ~/.gitconfig – ไฟล์ .gitconfig ใน home directory เก็บ git configurations สำหรับ specific user
  • .git/config – เก็บ git configuration สำหรับ local repository

ตรวจสอบ username และ email configuration ด้วยคำสั่ง:

git config --list

หากไม่ได้ output จากคำสั่งข้างต้น ตั้งค่า username และ email

ตั้งค่า global commit username และ email ใช้คำสั่ง:

git config --global user.name "Username"
git config --global user.email example@email.com

หลังคำสั่ง execute สำเร็จ ตรวจสอบ variables ที่ตั้งค่า:

git config --list

หลังรันคำสั่ง ควรได้ output คล้ายนี้:

user.name=Username
user.email=example@email.com
วิธีตั้งค่า Git Username และ Email ใน Ubuntu

สามารถ edit git config file เพื่อเพิ่ม username และ password ใช้ text editor โปรด edit "~/.gitconfig" เพิ่ม username และ email

nano /home/ubuntu/.gitconfig

เพิ่ม entries:

[user]
name = Username
email = example@email.com

Save file และใช้ git config command verify ว่าเพิ่ม entries สำเร็จ

วิธีตั้งค่า Local Git Username และ Email

Git อนุญาตให้ตั้งค่า local username และ email ใช้ local credentials สำหรับ specific repository

ทำได้โดยใช้คำสั่ง git config โดยไม่มี flag --global จากภายใน repository directory

ตัวอย่าง:

Navigate ไป directory ที่ต้องการใช้เป็น repo:

cd ~/workspace

Initialize directory เป็น git repository:

git init .

ภายใน repository ใช้คำสั่งด้านล่างตั้งค่า username และ email:

git config user.name "localusername"
git config user.email "user@ubuntu.local"

ตรวจสอบการเปลี่ยนแปลง:

cat .git/config

คำสั่งจะ navigate ไป .git directory ภายใน local repo และแสดง config file contents Git เก็บ configurations สำหรับ specific repo ใน .git/config

Output จะเป็น:

[core]
repositoryformatversion = 0
filemode = true
bare = false
logallrefupdates = true
[user]
name = localusername
email = user@ubuntu.local

แสดงทั้ง global และ local settings ด้วย git config ตัวอย่าง output:

user.name=Username
user.email=example@email.com
core.repositoryformatversion=0
core.filemode=true
core.bare=false
core.logallrefupdates=true
user.name=Localusername
user.email=user@ubuntu.local

Useful git config Commands

คำสั่ง git config ยังอนุญาตให้ตั้งค่า git settings อื่นๆ

ตัวอย่าง ตั้งค่า default git editor:

git config --global core.editor vim

แทนที่ vim ด้วย editor ที่ชอบ เช่น Emacs, nano ฯลฯ

เปลี่ยน default name สำหรับ initial branch – default เป็น "master"

ใช้คำสั่งด้านล่างเปลี่ยน default init branch name:

git config --global init.defaultBranch initial

แทนที่ "initial" ด้วยชื่อที่ต้องการสำหรับ init branch

ตรวจสอบ settings ทั้งหมด ตาม output ด้านล่าง:

user.name=Username
user.email=example@email.com
core.editor=vim
init.defaultbranch=initial
core.repositoryformatversion=0
core.filemode=true
core.bare=false
core.logallrefupdates=true
user.name=Localusername
user.email=user@ubuntu.local

สรุป

Git เป็น tool สุดโหด ช่วย users maintain, contribute, และ share work กับผู้อื่น เพื่อให้ efficient มากขึ้นเมื่อใช้ Git คุณสามารถใช้ Git Alias ปรับปรุง workflow หรือเรียนรู้วิธี delete local หรือ remote branch