Git Command 101

ปกติผมจะไม่ค่อยใช้ Command เลย นอกจากเขียนพวก Script ต่างๆ จะไป Google / Chat GPT เอาครับ คราวนี้มาลองสรุป Git แบบ Command กันครับว่ามีอะไรบ้าง

git init

  • git init : เอาไว้บอกว่า Folder นี้จะถูกใช้งานสำหรับการ Control Version เมื่อ Run แล้วจะได้ไฟล์ .git ขึ้นมา ข้างในมี
    - hooks #พวก scripts ที่ต้องการให้ทำงานตาม event เช่น ก่อน commit / push แต่ปกติเราใช้ .github / gitlab กัน
    - info : exclude file for ignored patterns
    - objects : ข้อมูลที่เราแก้ๆ จะอยู่ในนี้
    - refs : pointers ชี้บอก Commit จะได้รู้ว่าที่ทำไปมันอยู่ในจุดไหนของ Folder objects
    - config : configuration options
    - description :
    - HEAD :  current branch
    - index : staging area - อะไรที่เกิดจากคำสั่ง git add / git rm จะทดไว้ที่นี่

git config

  • git config <scope> <property> : กำหนดค่าของ git
git config -–global user.name "[user name]"

git config -–global user.email "[email address]"

git config --global user.password "[your password]" git เป็น https แต่ถ้าใช้เป็น ssh ไม่ต้องสนใจ 

git add

  • git add : เพิ่ม File / Folder ใน repository
#git add <file>
git add <file>

#Stage all changes in <specific path>
git add <specific path>

#Add All changes in 
git add .

git rm

  • git rm : ลบ File / Folder ออกจาก repository
#remove single file
git rm <file>

#--cached only remove in git repository, but keep it on the filesystem
$ git rm file2.txt --cached

#remove all file in folder
git rm <Path>/*

#use --dry-run for test, before execute
git rm css/* --dry-run

git status

  • git status: ตรวจสอบการเปลี่ยนแปลง (Change) หรือมีอะไรที่ยังไม่ได้เอาเข้า git (Untracked) ใน repository

git commit

  • git commit -m "Your Message" : ยืนยันการเปลี่ยนแปลงที่ทำขึ้น

git reset

  • git reset : clear หรือ Unstage ไฟล์ที่ git add / git rm ไป

git remote

  • git remote : เชื่อมความสัมพันธ์ระหว่าง folder ที่มี .git ของเครื่องเรา และ remote ที่ server
git remote add <<friendly_name>> <<remote repository url>>
#Example 
git remote add origin git@github.com:pingkunga/MergeDOTNETTestReport.git

#Verify new remote - ตรวจสอบ remote ที่ Link
git remote -v

> origin git@github.com:pingkunga/MergeDOTNETTestReport.git (fetch)
> origin git@github.com:pingkunga/MergeDOTNETTestReport.git (push)

git push

  • git push เอาการเปลี่ยนแปลงในเครื่องของเรา (Local) ส่งไปยัง Server (Remote)
git push <remote> <branch>

# --force เอา Local ทับ Remote
git push <remote> --force 

# --all เอาทุก Branch จาก Local ขึ้่นไป
git push <remote> --all

# --tags เอาทุก Tag จาก Local ขึ้่นไป
git push <remote> --tags
  • git push -u origin main อันนี้สงสัยเอง เพราะลองสร้าง Repo แล้วมันจะแนะนำมา โดยเจ้า -u เป็นการกำหนด upstream เวลาให้คำสั่งพวก git fetch / git merge / git pull / git status ไม่ต้องมาใส่ <remote> <branch> ในที่นี้ไม่ต้องมาใส่ origin main
-u , --set-upstream
For every branch that is up to date or successfully pushed, add upstream (tracking) reference, used by argument-less git-pull[1] and other commands. For more information, see branch.<name>.merge in git-config[1].

git clone

  • git clone: ดึง Repository จาก Remote ลงมาที่เครื่องของเราตาม Folder ที่กำหนดไว้ จากนั้นมันจะ Run git pull อีกที

git fetch / git merge / git pull

  • git fetch: ดึงสถานะล่าสุดจาก remote repository พวก refs / objects ใน .git
  • git merge: เอาที่เปลี่ยนแปลงจาก remote ที่ git fetch ดึงเก็บไว้แล้ว มา Apply กับ Folder ในเครื่องของเรา
  • git pull: git fetch + git merge

git branch

  • branch มองเหมือนการสร้างพื้นที่ทำงานเพิ่ม โดยที่ไม่กระทบกับพื้นที่ทำงานหลัก เช่น ตอนนี้งานบน Production อยู่ที่ master ถ้าจะไปแก้อะไรตรงๆก็เสี่ยงไป เลยต้องแยกพื้นที่ออกมา feature/34579_swappricing เพื่อทำงานของเราครับ
  • git branch : List Local Branch
  • git branch -r : List Remote Branch
  • git branch <<branch_name>> สร้าง branch ใหม่
  • ถ้าจะเปลี่ยน branch ใช้คำสั่ง git switch
  • จากนั้นลอง run git branch อีกรอบ จะเห็นว่า ตัว * บอก Working Directory ของเราในปัจจุบัน
  • git branch --delete <<branch_name>> ลบ branch ออกไป แต่ต้อง switch directory ก่อน ถึงจะลบได้

git switch / git checkout

  • git switch <<branch_name>> : เปลี่ยน branch หรือจัดการ Branch (Requied Git version 2.23)

git switch มันมีจุดเด่นเหนือกว่า git checkout มันออกแบบมาแก้ปัญหา แจ้งเตือนกรณีมีไฟล์แก้ และยังไม่ Commit

  • git checkout -b <<branch_name>> เปลี่ยน branch ถ้าไม่มีก็สร้างให้ด้วยเลย
git checkout -b feature/350896_eodSBL

อ๋อ แล้วเจ้า git checkout มันมีความสามารถหลายอย่างเลย ลองไปดูได้จากอันนี้ https://stackoverflow.com/a/70454786/20000255

ปิดท้าย

สำหรับความสัมพันธ์ของ Local / Remote จาก Action ต่างๆ สามารถดูรูปนี้ได้เลย

Ref: Reverting Modified in 4 Stages in Git | by Jianping Zeng | Medium

จริงๆ ตัว Git Command มีมากกว่านี้อีกครับ อย่างพวก git tag / git slash เป็นต้น อันนี้ลองให้เห็นภาพ จากเดิมที่ผมกดจาก UI หรือหน้าเว็บ เปลี่ยนมาให้ Command แทน
ถ้าต้องการศึกษาเพิ่มเติม ลองไปเล่นจาก Interactive Web ก็ได้นะ Learn Git Branching

Reference


Discover more from naiwaen@DebuggingSoft

Subscribe to get the latest posts sent to your email.