คำสั่งต่าง ๆ ของ Git
#HelloFront-end #HelloErmine #HelloWorld2021
git config
git config
เป็นคำสั่งที่ใช้แสดงและกำหนดข้อมูลของผู้ใช้เพื่อระบุตัวตน และคุณสมบัติอื่น ๆ ของ Git
$git config --global --list #แสดงคุณสมบัติของ Git ทั้งหมด
$git config --list #แสดงคุณสมบัติของ Git เฉพาะ repository นั้น
$git config --global user.name "Your Name" #กำหนดชื่อผู้ใช้
$git config --global user.email "example@email.com" #กำหนดอีเมลของผู้ใช้
$git config --global --list #ตรวจสอบอีกครั้งหลังจากกำหนดค่าเสร็จแล้ว
git init
git init
เป็นคำสั่งที่ใช้สร้างระบบของ Git ขึ้นมาภายใต้โฟลเดอร์หรือ path นั้น โดยจะสร้างโฟลเดอร์ .git ขึ้นมาเพื่อใช้เก็บสำรองข้อมูลการเปลี่ยนแปลงและคุณสมบัติอื่น ๆ ของ Git
$git init
git status
git status
เป็นคำสั่งที่ใช้ตรวจสอบสถานะของ source code ในระบบของ Git ซึ่งจะแสดงสถานะที่ได้อธิบายข้างต้นไปแล้วเรื่อง สถานะต่าง ๆ ของ Git
$git status
git add
git add
เป็นคำสั่งที่ใช้เพิ่มการเปลี่ยนแปลงของ source code เข้าไปที่สถานะ Staged
$git add <file_name>
$git add README.md #เพิ่มไฟล์ชื่อ README.md เข้าไปที่สถานะ Staged
$git add . #ใช้ในกรณีที่มีหลาย ๆ ไฟล์และต้องการเพิ่มเข้าไปทั้งหมด
git commit
git commit
เป็นคำสั่งที่ใช้ยืนยัน source code ที่อยู่ในสถานะ Staged เข้าไปเก็บไว้ที่ Local Repository
$git commit -m "message" #ยืนยันการเปลี่ยนแปลงพร้อมข้อความ
$git commit -am "message" #เพิ่มการเปลี่ยนแปลงและยืนยันพร้อมข้อความ
$git commit #เพิ่มข้อความในโปรแกรม VI
#ยืนยันการเปลี่ยนแปลงพร้อมข้อความและ merge ลงใน commit ล่าสุด
$git commit --amend -m "message"
git log
git log
เป็นคำสั่งที่ใช้แสดงประวัติการ commit ที่เก็บไว้ใน repository
$git log
$git log --oneline #แสดงข้อมูลแบบบรรทัดเดียว
$git log --stat #แสดงข้อมูลพร้อมแสดงการเปลี่ยนแปลงของข้อมูล
$git log --grep="Message" #แสดงข้อมูลโดยเลือกจากข้อความที่ commit
$git log --after="2017-2-14" #แสดงข้อมูลหลังวันที่นั้น ๆ
$git log --before="2017-2-14" #แสดงข้อมูลก่อนวันที่นั้น ๆ
$git log --author=pakin #แสดงข้อมูลโดยเลือกจากผู้ commit
git branch
git branch
เป็นคำสั่งที่ใช้ในการแสดง branch และสร้าง branch เพื่อแยกสาขาในการพัฒนา ซึ่งทำให้การพัฒนาซอฟต์แวร์มีความยืดหยุ่นมากขึ้น
$git branch
$git branch --all
$git branch develop #สร้าง branch ชื่อ develop
$git branch --delete develop #ลบ branch ชื่อ develop
#ส่งการเปลี่ยนแปลง branch develop ไปยัง remote ที่ชื่อ origin
$git push origin develop
#ส่งการเปลี่ยนแปลงลบ branch develop ไปยัง remote ที่ชื่อ origin
$git push --delete origin develop
เรื่องของ branch และ tag มีความเกี่ยวข้องกับเรื่องของ Release Process ของการพัฒนาซอฟต์แวร์ ขึ้นอยู่กับการตกลงกันภายในทีมและรูปแบบที่เหมาะสมกับซอฟต์แวร์ที่กำลังพัฒนา ซึ่งเรียกเทคนิคนี้ว่า Branch Strategy (Git Workflow, Branching Models, Branching Workflow, Git Flow)
git checkout
git checkout
เป็นคำสั่งที่ใช้ในการสลับ Working Directory ไปยัง branch หรือ commit ที่เราระบุ
#ย้ายการทำงานไปที่ branch หรือ commit_id ที่ระบุ
$git checkout <branch name, commit_id>
#สร้าง branch ชื่อ test และทำการสลับการทำงานมาที่ branch test
$git checkout -b test
#ยกเลิกการเปลี่ยนแปลงของไฟล์ใน Working Directory
$git checkout -- <file name>
#เลือกแค่บางไฟล์จาก branch อื่น เข้ามา merge กับ Working Directory ที่กำลังทำงาน
$git checkout <branch name> <file name>
#คำสั่งนี้จะเหมือนคำสั่งด้านบนแต่จะมีโหมดตอบโต้กับผู้ใช้ในการเลือกสถานะของไฟล์ที่ระบุ
$git checkout --patch <branch name> <file name>
git reset
git reset
เป็นคำสั่งที่ใช้ย้อนกลับไปที่เวอร์ชัน ก่อนหน้า โดยระบุ branch หรือ Commit Id (SH-1 แบบย่อของ Commit 7 ตัว เช่น 4bcb295) ซึ่งมี option ที่สำคัญ 3 ตัวดังนี้
soft ย้อนการเปลี่ยนแปลง และคงสถานะการเปลี่ยนแปลงของ source code ไว้ที่สถานะ Staged
mixed ย้อนการเปลี่ยนแปลง และคงสถานะการเปลี่ยนแปลงของ source code ไว้ที่สถานะ Working Directory หรือ Modified
hard ย้อนการเปลี่ยนแปลงแบบลบทับการเปลี่ยนแปลงก่อนหน้าทั้งหมด คำสั่งนี้อันตรายเพราะมันจะทำให้ประวัติของ commit ที่เก็บไว้ใน repository หายไป จึงยังไม่เหมาะกับมือใหม่
$git reset --soft 4bcb295 #ย้อนกลับไปที่ Commit id 4bcb295
$git reset --mixed develop #ย้อนกลับไปที่ branch ที่ชื่อว่า develop
git merge
git merge
เป็นคำสั่งที่ใช้ในการรวม branch หรือ commit ทั้งสองเข้าด้วยกัน
ตัวอย่าง เราจะอยู่ที่ branch master และต้องการ merge branch feature เข้ามาทำงานร่วมด้วย การ merge แบบ No Fast Forward หรือเรียกอีกอย่างหนึ่งว่า 3-Way Merge
#รวม branch master กับ branch feature แบบ No Fast Forward
$git merge --no-ff feature
#รวม branch master กับ branch feature แบบ Fast Forward
$git merge feature
git remote
git remote
มักใช้งานเมื่อมีการทำงาน Git อยู่ที่ Local Repository ตั้งแต่แรก หรือต้องการที่จะเปลี่ยน Remote Repository ใช้งานโดยการเพิ่ม URL ของ Remote Repository เข้าไปยังคุณสมบัติของ Git โดยชื่อว่า origin ส่วนใหญ่จะเป็นชื่อ default ที่หลาย ๆ คนเข้าใจตรงกัน แต่เราก็สามารถตั้งชื่ออื่น ๆ ได้
$git remote add origin <URL> #เพิ่ม Remote Repository ชื่อ origin
$git remote add origin https://github.com/NewGame0/Android_HelloWorld.git
#เพิ่ม Remote Repository ใหม่ ชื่อ origin
$git remote set-url origin <New URL>
$git remote -v #แสดง Remote Repository
$git config --list #แสดงคุณสมบัติต่าง ๆ ของ Git ซึ่งจะมี Remote Repository แสดงออกมาด้วย
git push
git push
เป็นคำสั่งที่ใช้ส่งการเปลี่ยนแปลงของ source code ที่เก็บอยู่บน Local Repository ขึ้นไปยัง Remote Repository
#ส่งการเปลี่ยนแปลง branch master ไปยัง remote ที่ชื่อ origin
$git push origin master
git fetch
git fetch
เป็นคำสั่งที่ใช้รับการเปลี่ยนแปลงของ source code ล่าสุดที่อยู่บน Remote Repository ลงมายัง Local Repository แต่ยังไม่ได้ทำการรวม source code (merge)
#รับการเปลี่ยนแปลงทุก branch จาก Remote Repository
$git fetch --all
#รับการเปลี่ยนแปลง branch master จาก Remote Repository ที่ชื่อ origin
$git fetch origin master
git pull
git pull
[fetch + merge] เป็นคำสั่งที่ใช้รับการเปลี่ยนแปลงของ source code ล่าสุดที่อยู่บน Remote Repository ลงมายัง Local Repository และทำการ auto merge
$git pull <remote> <branch>
$git pull origin master
git clone
git clone
เป็นคำสั่งที่ใช้ดึงประวัติทั้งหมดบน Remote Repository ของเพื่อนร่วมทีม ของคนอื่น หรือของเราเอง ที่มีอยู่แล้วบน Git Hosting มาที่เครื่องของเรา คำสั่งนี้จะคล้าย ๆ git init ที่ใช้สร้างระบบ Git ขึ้นมาตอนเริ่มต้น แต่เราจะได้ประวัติเดิมของ repository มาด้วย ทำให้เราเริ่มพัฒนาต่อจากตรงจุดนี้ได้เลย
$git clone https://github.com/NewGame0/Android_HelloWorld.git
คำสั่ง git clone นั้นจะ checkout branch หลักมาเป็น master และดึง tag ลงมาทั้งหมด

git tag
git tag
เป็นคำสั่งที่ใช้แสดงและสร้าง tag ชื่อขึ้นที่จุด commit นั้น ๆ เปรียบเสมือนการตั้งชื่อขึ้นที่ commit ต่างจากการใช้ git comit -m "message" จะเป็นข้อความเพื่ออธิบายการ commit นั้น
$git tag #แสดง tag ทั้งหมด
$git tag -n99 #แสดง tag ทั้งหมดพร้อมข้อความ
$git tag v1.0.0 #สร้าง tag ชื่อ v1.0.0
$git tag v1.0.1 -m "Tag Message" #สร้าง tag ชื่อ v1.0.0 พร้อมระบุข้อความ
$git tag --delete v1.0.0 #ลบ tag ชื่อ v1.0.0
$git push origin <tag name> #ส่ง tag ขึ้นไปที่ Remote Repository
$git push origin --tags #ส่ง tag ทั้งหมดขึ้นไปที่ Remote Repository
$git push --delete origin <tag name> #ลบ tag ที่ Remote Repository
git clean
git clean
เป็นคำสั่งที่ใช้แสดงและลบ source code ที่อยู่ในสถานะ Untracked ออกจาก Working Directory
$git clean -n #แสดง source code ที่อยู่ในสถานะ Untracked
$git clean -df #ลบ source code ที่อยู่ในสถานะ Untracked
git diff
git diff
เป็นคำสั่งที่ใช้แสดงความเปลี่ยนแปลงระหว่าง Working Directory ที่กำลังทำงานอยู่กับ branch หรือ Commit Id ที่ระบุ ตัวอย่างเช่น เราทำงานอยู่ที่ branch develop แล้วทำการเพิ่ม code เมื่อใช้คำสั่ง git diff จะแสดงให้เห็นว่าก่อนหน้านี้เป็นอย่างไรและมีอะไรเพิ่มขึ้นมาบ้าง
$git diff 82de188
$git diff develop
git stash
git stash
เป็นคำสั่งที่ใช้ซ่อนการเปลี่ยนแปลงใน Working Directory นิยมใช้ก่อนคำสั่ง git pull
$git stash #ซ่อนการเปลี่ยนแปลงลงใน stash
$git stash list #แสดงรายการการเปลี่ยนแปลงที่ซ่อนไว้
$git stash show #แสดงการเปลี่ยนแปลงล่าสุดที่ซ่อนไว้
$git stash pop #ดึงการเปลี่ยนแปลงล่าสุดมาออกมา merge กับ Working Directory
git reflog
git reflog
เป็นคำสั่งที่ใช้แสดงและจัดการกับ Reference Log ของ git repository ส่วนมากใช้กับการกู้ข้อมูลใน commit เมื่อเผลอ git reset ผิด ยกตัวอย่างเช่น ใช้คำสั่ง git reset -hard ย้อนกลับไป 3 commit ก่อนหน้า ทำให้ประวัติของ commit ทั้ง 3 ก่อนหน้าที่จะย้อนมาหายไป เราจะใช้คำสั่งตามด้านล่างนี้
#แสดง Reference Log ของ git repository
$git reflog show
#ย้อนกลับไปยัง head log ก่อนหน้า เท่านี้ก็จะได้ 3 commit กลับมาแล้ว
$git reset HEAD@{1}
#ตัวอย่างการแสดงผลของ Reference Log
f3918be (HEAD -> master) HEAD@{0}: reset: moving to f3918be770c8630e2117b194a12bedd8e5fb7417
772f7ca HEAD@{1}: reset: moving to 772f7ca0903c0a8e13fdfe7e66e9734b255bfc97
f3918be (HEAD -> master) HEAD@{2}: reset: moving to f3918be770c8630e2117b194a12bedd8e5fb7417
Git Ignore
Git Ignore
Git Ignore [.gitignore] ไม่ได้เป็นคำสั่งแต่เป็นคุณสมบัติของ Git โดยการเพิ่มไฟล์ที่ชื่อ .gitignore เข้าไปในระบบของ Git เพื่อทำการบอกให้ Git ไม่ต้องสนใจไฟล์หรือโฟลเดอร์นั้น ๆ
เช่น ไฟล์หรือโฟลเดอร์ที่เป็น output ของการ Build ใน Java (.class) ไฟล์ที่เป็นคุณสมบัติเฉพาะของ IDE หรือ Working Space ก็ไม่ควรแชร์ไปให้คนอื่น ๆ ในทีม
$touch .gitignore #สร้างไฟล์ .gitignore
#เพิ่ม String เข้าไปในไฟล์ .gitignore เพื่อ ignore ไฟล์ .class ทั้งหมด และโฟลเดอร์ Debug, Build
$echo >> .gitignore "*.class" #ทุกไฟล์ที่เป็นนามสกุล .class
$echo >> .gitignore "/Debug" #ทุกไฟล์ที่อยู่ในโฟลเดอร์ Debug
$echo >> .gitignore "/Build" #ทุกไฟล์ที่อยู่ในโฟลเดอร์ Build
$git add .gitignore #เพิ่มไฟล์ชื่อ .gitignore เข้าไปที่สถานะ Staged
$git commit -m "Add .gitignore file"
ในกรณีที่มีการเพิ่มไฟล์ที่ไม่ต้องการเข้าไปยังสถานะ Staged แล้ว และเพิ่มไฟล์ .gitignore เข้าไปทีหลัง สามารถใช้คำสั่งนี้เพื่อลบไฟล์หรือโฟลเดอร์ที่ไม่ต้องการออกจากสถานะ Staged และ commit .gitignore เข้าไปอีกครั้ง
$git rm --cached <file name>
$git rm --cached <path to file>
$git rm --cached .class
$git rm --cached Debug/*
$git rm --cached Build/*
$git rm -r --cached *
$git commit -am "apply .gitignore file"
#แสดงไฟล์ที่ติดตามไว้ในระบบของ Git ไฟล์ที่ ignore จะหายไปแม้จะยังอยู่ในโฟลเดอร์
$git ls-files
Last updated
Was this helpful?