youtube :
git config --global user.email "you@example.com"
git config --global user.name "Your Name"
ตั้งค่า default branch
git config --global init.defaultBranch main
ตรวจสอบการ config user
git config --list
คำสั่ง Git พื้นฐาน
git init สร้าง respository ใหม่ (U)
git log ดู history หรือประวัติการ commit ของ respository
git status ตรวจสอบสถานะของ repo ประกอบด้วยไฟล์ต่างๆ ทั้งที่ยังไม่ได้เพิ่มใน staged และเพิ่มไไปยัง staged แล้ว
git add . เพิ่มไฟล์ทั้งหมดเข้า staging area (A)
git add *.js เพิ่มไฟล์แบบระบุนามสกุล
git reset นำเอาไฟล์ต่างๆออกจาก staging area
git restore --staged a.js นำเอาไฟล์ a.js ออกจาก staging area (git reset)
git restore <ชื่อไฟล์> ย้อนกลับ code ในไฟล์นั้น (Undo)ใช้ใน working directory
git restore . ย้อนกลับ code ในไฟล์ทั้งหมด (Undo) ใช้ใน working directory
git commit -m "init project" save เข้า local git repository
git commit -am "init project" ไม่ต้อง git add ทำพร้อมกัน
git commit --amend -m "New commit message" แก้ไขข้อความใหม่
git reset --mixed IdIndex ย้อนกลับการเปลี่ยนแปลง และกลับไปยัง Woking Directory (โค้ดเดิมไม่หาย)(default)
git reset --soft IdIndex ย้อนกลับการเปลี่ยนแปลง และกลับมายัง Staged (โค้ดเดิมไม่หาย)
git reset --hard ย้อนกลับการเปลี่ยนแปลงพร้อมทั้งลบโค้ดเดิมที่เคยเขียนไว้ รวมถึงประวัติการ commit ด้วย (ต้องระวัง มือใหม่ไม่แนะนำ)
การเขียน commit ที่ดีคือเขียนให้ตัวเองและทีมเข้าใจ และเป็นหน่วยย่อย
คำสั่งที่ใช้เกี่ยวกับ Branch
git branch แสดง branch ทั้งหมดที่มีใน repo พร้อมทั้งบอกด้วยว่าตอนนี้กำลังใช้งาน branch ไหนอยู่ (สังเกตจากเครื่องหมาย *)
git branch -v โชว์ commit ล่าสุดด้วย
git branch <ชื่อ> สร้าง branch ใหม่
git checkout <ชื่อ> สลับมาใช้ branch ชื่อนี้
git checkout -b <ชื่อ> checkout พร้อมกับสร้าง branch ใหม่เลย (รวมสองคำสั่ง) ก็ได้
git branch -m <ชื่อเดิม> <ชื่อใหม่> เปลี่ยนชื่อ (rename) branch
git merge <ชื่อ> merge หรือรวม branch (นิยมใช้กับ production branch)
git merge --no-ff <ชื่อ> (เหมาะกับ development branch)
git branch -d <ชื่อ> ลบ branch ชื่อนี้ (ต้อง merge ก่อนถึงจะลบได้)
git branch -D <ชื่อ> ลบ branch ชื่อนี้ (ไม่ต้อง merge ก่อนก็ลบได้)
git log --graph --oneline แสดงประวัติในรูปแบบ graphical
คำสั่ง Stashing
ในบางครั้งเรากำลังทำงานที่ branch ต่างๆ อยู่ และยังไม่เสร็จ (ยังไม่ได้ commit) แต่ดันมีงานด่วนที่ต้องไปทำก่อนหรือไปทำที่ branch อื่นก่อน เราสามารถใช้คำสั่ง git stash เพื่อเก็บสิ่งที่ทำอยู่แล้วนำกลับมาทำต่อได้ในภายหลัง
*ต้อง git add . ก่อน
git stash
git stash list เอาไว้ดู
git stash pop เอาออกมาใช้
git stash show stash@{0} (เลือกตาม id)
คำสั่งเกี่ยวกับการจัดการ Remote Repository
Copy Git repository จาก remote ด้วยคำสั่ง git clone (สร้าง repo ที่ remote ก่อน)
git clone git@gitlab.com:codingthailand/workshop-git-remote.git
git remote หรือ git remote -v แสดงชื่อ และ url ของ remote repo
git remote add <remote name> <remote url> เพิ่ม ชื่อ และ url ของ remote repo
Ex. git remote add origin git@gitlab.com:codingthailand/workshop-git-remote.git
git push -u <ชื่อ branch remote> <ชื่อ branch local> ส่งไฟล์ทั้งหมดของ branch นี้ไปยัง remote repo ต่อไปสามารถใช้คำสั่ง git push เฉยๆ ได้ (-u ใช้เฉพาะครั้งแรก)
Ex. git push -u origin main , git push -u origin --all
git fetch <ชื่อ branch remote> ตรวจสอบว่าที่ remote มีใครแก้อะไรหรือไม่(ไม่มีการ merge ต้องสั่ง merge เอง) เช่น git fetch origin
git pull <ชื่อ branch remote> <ชื่อ branch local> ตรวจสอบว่าที่ remote มีใครแก้อะไรหรือไม่ พร้อมกับ merge local branch ปัจจุบันที่กำลังทำงานอยู่ (ควรทำทุกครั้งก่อนแก้ไขงานใหม่) เช่น git pull origin main
git remote remove <ชื่อ branch remote> ลบ remote URL จาก local repository เช่น git remote remove origin
- การสร้าง tag เวอร์ชันนด้วยคำสั่ง git tag -a 1.0.0 -m "1.0.0 stable is here"
หลักแรกคือเลขเวอร์ชันหลัก
หลักสองคือเลขการเพิ่มฟีเจอร์
หลักสามคือการแก้ bug
- ให้ push production พร้อม tag ไปที่ remote repo ด้วยคำสั่ง (อย่าลืม push main)
git push -u origin product --tags
หรือ push ทั้งหมด
git push -u origin --all
git push -u origin --tags