ขอเกริ่นไว้นิดนึงสำหรับคนที่ไม่รู้จัก Git นะครับ Git คือ Version Control ที่ไว้ช่วยเก็บรักษาไฟล์หรือแชร์ Source Code ให้ผู้อื่นได้ สามารถย้อนกลับไปยัง Version ต่างๆได้ หรือ Track ได้ว่าใครเปลี่ยนแปลงแก้ไข Code หรือเพิ่มไฟล์
จริงๆข้อดีมีเยอะแยะมากมาย แต่บทความนี้ขอเน้นการใช้งานคำสั่ง Git พื้นฐานและใช้บ่อยเป็นหลักครับ
git init
สำหรับสร้าง Git Repository ให้กับโปรเจ็คของเรา โดยเมื่อสร้าง Directory ขึ้นมาแล้วให้เข้าไปใน Directory และใช้คำสั่ง
git init
git clone
กรณีมีโปรเจ็คใน Repository อยู่แล้ว เราสามารถดึงลงมาในเครื่องเราได้ 2 รูปแบบ
# ดึงข้อมูลจาก Repository ที่อยู่บนเครื่องของเราเอง
git clone <remote_url>
# ดึงข้อมูลจาก Repository จากเครื่องอื่น
git clone <username>@<host>:<path_to_repository>
git config
การตั้งค่า Git โดยใส่ชื่อและอีเมล์
git config --global user.name "your_name"
git config --global user.email "[email protected]"
แสดงสถานะการตั้งค่า
git config --list # แสดงเฉพาะ Repository นี้
git config --global --list # แสดงทั้งหมด
ตั้งค่าให้ git บันทึก username, password จำไว้ในเครื่องเราเอง เพื่อไม่ให้ถาม username, password ในตอนเวลาเรา pull โค้ดลงมา
git config credential.helper store # จำเฉพาะ Repository นี้
git config --global credential.helper store # จำ Repository ทั้งหมด
git add
คำสั่งเพื่อบันทึกเปลี่ยนสถานะ (Stage) รอการ Commit
git add <file_name> # ระบุไฟล์ เช่น git add index.html about.html
git add . # ทุกไฟล์ที่อยู่ภายใต้ Directory ปัจจุบัน
git add --all # ทุกไฟล์ใน Project (หรือ git add -A)
git add *.html # หลายไฟล์ระบุนามสกุล
git commit
คำสั่งเพื่อบันทึกไฟล์ทั้งหมด ขั้นตอนนี้จะ Commit ไปที่ Repository ภายในเครื่องของเรา (Local) ส่วน -m เป็น Option เพื่อใส่ข้อความว่าได้แก้ไขอะไรลงไปบ้าง
git commit -m "ข้อความอธิบายการเปลี่ยนแปลง"
# หรือจะรวมทั้ง add และ commit ในคราวเดียวก็ใช้ -am
git commit -am "ข้อความอธิบายการเปลี่ยนแปลง"
กรณีที่ commit ไปแล้ว แต่ลืมและมีแก้อะไรเล็กน้อย ทำให้ต้อง commit เพิ่มเติมเข้าไป ให้แก้ไขโดยเพิ่ม --amend เข้าไปด้วย เพื่อที่มันจะได้รวบเป็น commit เดียวกัน
git commit -m --amend "ข้อความอธิบายการเปลี่ยนแปลง"
# หรือ
git commit -am --amend "ข้อความอธิบายการเปลี่ยนแปลง"
git push
เพื่อส่งไฟล์ขึ้นไปที่ Repository บน Git (Remote)
git push <remote_name> <branch_name> # ตัวอย่าง git push origin master
# ลบ Branch บน Remote
git push origin --delete <branch_name>
# ส่งไฟล์พร้อม Tag
git push origin <tag_name> # แบบระบุ tag
git push origin --tags # tag ทั้งหมด
git stash
คำสั่งเพื่อซ่อนการเปลี่ยนแปลงชั่วคราว ประโยชน์คือในขณะที่เรากำลังทำงานแล้วบังเอิญมีงานแก้ Bug ด่วนแทรกเข้ามา Git stash จึงเข้ามาเพื่อช่วยเก็บซ่อน Code นี้ไว้ก่อนในเครื่องเราชั่วคราว
git stash
git stash -u # ให้ stash ไฟล์ที่ untracked ไปด้วย
git stash list # ดูรายการทั้งหมด
git stash pop # นำ Code ที่ซ่อนไว้กลับมาทำต่อ
git stash clear # Clear รายการที่ถูกซ่อน
git reset
คำสั่งยกเลิกเมื่อเราสั่ง git add ไปแล้ว
git reset <file_name>
คำสั่งยกเลิกเมื่อเราสั่ง git commit ไปแล้วบน Local
# --hard คือ ลบสิ่งที่เคย commit ออกไปเลย กลับไปยัง commit ก่อนหน้า
git reset --hard HEAD~1
# --soft คือ นำสิ่งที่เคย commit กลับมายังสถานะ staged เหมือนก่อน commit
git reset --soft HEAD~1
git rm
คำสั่งลบไฟล์หรือโฟลเดอร์ออกจาก git (ไม่ใช่การลบไฟล์ออกจากโปรเจ็ค)
git rm <file_name> # ลบไฟล์
git rm -r <folder_name> # ลบโฟลเดอร์
# ลบไฟล์ที่ถูกทำดัชนี หรือลบออกจาก stage
git rm -r --cached . # ทั้งหมด
git rm --cached <file_name> # ระบุไฟล์
git status
คำสั่งเพื่อดูสถานะการเปลี่ยนแปลงของ Repository บนเครื่องเรา
git status
git diff
ใช้สำหรับดูว่า Code มีอะไรเปลี่ยนแปลงและแตกต่างไปบ้าง
git diff # เฉพาะ Branch หรือ Commit ปัจจุบัน
git diff <commit_id> # แบบระบุ Commit ID
git diff <commit_id> <commit_id> # เปรียบเทียบระหว่างสอง Commit
git log
คำสั่งใช้ดูประวัติการ commit ต่างๆ ของ Repo
git log
git log --oneline # แสดงแต่ละ log เหลือบรรทัดเดียว
git log --pretty=oneline # แสดงแต่ละ log เหลือบรรทัดเดียว แต่แสดง commit เต็ม
git log --graph # แสดงเป็นเส้น Branch ให้ดูง่ายขึ้น
git log --oneline --graph # ดูง่ายมาก
สำหรับแสดงแบบมีสี บอกวัน และคนที่ Commit รวมถึงเส้น Branch:
git log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit
คำสั่งนั้นค่อนข้างยาว ดังนั้นให้เพิ่ม Alias ไว้ใช้งาน:
git config --global alias.logline "log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit"
เวลาจะเรียกดูก็ใช้คำสั่งสั้นๆ
git logline
git fetch
เพื่อใช้ดึงความเปลี่ยนแปลงจาก Remote Repository มายัง Local Repository
git fetch
git pull
คำสั่งดึงไฟล์หรืออัพเดท Source Code ภายในเครื่อง (Local) ให้ตรงกับ Repository (Remote) โดย git pull จะทำการ git fetch และ git merge ไปด้วย
git pull <remote_name> <branch_name> # ตัวอย่าง git pull origin master
git branch
Branch มีเพื่อเราจะได้แยกการพัฒนาออกจาก Branch หลัก โดย Branch เริ่มต้นจะชื่อว่า master
git branch <branch_name> # สร้าง Branch
git branch # ดูรายชื่อ branch ทั้งหมด
git branch -d <branch_name> # ลบ Branch (Local)
เครื่องหมายดอกจัน * หมายถึงเรากำลังอยู่ที่ Branch นั้น
* master
dev
fixbug
git checkout
git checkout <branch_name> # ไปยัง Branch ที่มีอยู่แล้ว
git checkout -b <branch_name> # สร้าง Branch ใหม่และสลับไปทันที
git checkout -b <branch_name> <commit_id> # สร้าง Branch ใหม่จาก Commit ID
git checkout -- <file_name> # ย้อนกลับไปยัง Commit ล่าสุด (ยกเลิกการแก้ไขไฟล์)
git merge
git merge <branch_name> # รวม Branch มายัง Local
git merge --no-ff <branch_name> # รวม Branch มายัง Local (ไม่ใช่เป็น fast-forward)
git remote
git remote -v # แสดงข้อมูล URL ของ Repository
git remote add origin <remote_url> # เพิ่ม URL ของ Repository
git remote set-url origin <remote_url> # เปลี่ยน URL ของ Repository
git remote rename <old_name> <new_name> # ตั้งชื่อ Remote ใหม่
git remote rm <remote_name> # ลบ Remote
git tag
git tag # แสดง Tag ทั้งหมด
git tag --sort=-creatordate # แสดงเรียงจากวันที่สร้างล่าสุด
git tag -l "keyword" # ค้นหา Tag
git tag -l "keyword*" # ค้นหา Tag ตามด้วยอะไรก็ได้
git tag -a <tag_name> -m "หมายเหตุ" # สร้าง Tag
git clean
git clean -n # แสดงไฟล์ที่อยู่ในสถานะ Untracked
git clean -df # ลบไฟล์ที่อยู่ในสถานะ Untracked
git version
git version
git help
git help # ดูว่ามีคำสั่งอะไรบ้าง
git help <command_name> # ดูวิธีการใช้งานของคำสั่งนั้นๆ
คำสั่งอาจจะไม่ครบถ้วน ถ้ามีคำสั่งอื่นๆก็จะมาอัพเดทในบทความนี้ที่เดียวเลยอีกครั้ง หวังว่าจะมีประโยชน์ครับ