Skip to main content

คำสั่งพื้นฐาน Git

Step ของ git มีอะไรบ้าง ?

ตอนเราสร้าง Project มานั้น เราจำเป็นจะต้องบอก repository (ซึ่งก็คือ folder, directory project เรานั่นแหละ) ก่อนว่า เราจะขอให้ repository นี้เป็น Git

เริ่มลองคำสั่งพื้นฐาน

ในส่วนแรก คือคำสั่งพื้นฐานของ git ที่จะทำให้เราเข้าใจ flow git มากขึ้น เริ่มต้นเราจะ

  1. เปลี่ยน folder ให้เป็น git ด้วย git init
  2. เริ่ม add file (stage change) เข้า git ด้วย git add
  3. เช็คการเปลี่ยนแปลง ณ ขณะนั้นของ git ด้วย git status
  4. นำ file ที่ add commit (stage change) ของ git เก็บ commit stage change ไว้ด้วย git commit
  5. เช็ค history ของการ commit git ด้วย git log

1. git init

  • เริ่มต้น เข้า folder (directory) สร้างไฟล์ index.html (ซึ่งจะเป็นส่วนหนึ่งของการทดสอบนี้) ใส่ข้อมูลพื้นฐานง่ายๆไป

ที่ index.html

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
Hello world
</body>
</html>

หลังจากนั้นใช้คำสั่ง

git init

เราจะได้ folder .git มา ซึ่ง folder นั้นแหละจะใช้สำหรับเก็บ git history (จากการ commit) เอาไว้ได้

หน้าตาจะประมาณนี้ (มันจะไม่โชว์ใน vscode แต่สามารดูผ่าน command ได้)

intro-4

2. git add

# add แบบ file เดียว
git add <file>

# add แบบทั้งหมด
git add -A

3. git status

git status

4. git commit

# ทำการ shapshot พร้อม commit message
git commit -m '<commit message>'

# ทำการ add พร้อมกับทำการ snapshot message
git commit -am '<commit message>'

5. git log

git log

วิธีการดู git บน vscode (เพิ่มเติม)

  • git change ดูได้จากตรง tab git ใน vscode
  • History สามารถทำได้จากการลง plugin ใน vscode Git lens, Git history