การกำหนดค่าเริ่มต้น

git config — global user.name “xxx”
git config — global user.email “xxx@xxx.com

ดูค่าที่ config

git config — list

การแก้ไขที่ไฟล์

.gitconfig ที่ home directory

สร้าง Repository

ไปที่ Project Directory

git init

จะทำการสร้าง .git (hidden directory)

ตรวจสอบใช้คำสั่ง ls -a (dir /a บนวินโดวน์)

การสร้างไฟล์สำหรับการ track

git add *.xxx
git add *.*
git add .

xxx เป็นนามสกุลไฟล์ในการ track

git add readme.txt

เพิ่มไฟล์ readme ในการ track (ชื่อไฟล์เป็น readme มีนามสกุลหรือไม่มีนามสกุลไฟล์ก็ได้) จะถูกนำไปใช้ในการแสดงรายละเอียดของ Repository

git commit -m “initial project” 

การคัดลอก Existing Repository

git clone git://github.com/naruepol/javaproject.git 

การคัดลอก Exitsting Repository พร้อมตั้งชื่อในการอ้างถึง

git clone git://github.com/naruepol/javaproject.git javaproject

Lifecycle ของสถานะไฟล์

  • Untracked
  • Unmodified
  • Modified
  • Staged

ตรวจสอบ สถานะของไฟล์

git status

ตรวจสอบประวัติการ commit

git log
git log — since=2.weeks

แก้ไขข้อความในการ commit ก่อนหน้า

git commit — amend

แก้ไขข้อความการ commit โดยระบุลงไปในคำสั่ง

git commit — amend -m “updated commit message”

ดูรายละเอียดการ commit ทีมีการแก้ไข

git reflog

ดูรายละเอียดการ commit ทีมีการแก้ไข (บอกรายละเอียดของเวลา)

git reflog — relative-date

ขั้นตอนการดำเนินการควบคุมเวอร์ชันเบื้องต้น

  • add
  • commit

การนำไฟล์ออกจากการ track

git rm — cached xxx

xxx คือไฟล์ที่ต้องการนำออก จาก git

git rm xxx

การนำออกจาก git และลบไฟล์

การกำหนดไฟล์ที่ไม่ต้องการ track โดยใช้ .gitignore

ตัวอย่างข้อมูลในไฟล ์ .gitignore

*.xxx 
*.txt
!note.txt

!note.txt หมายความว่ายกเว้นไฟล์ note.txt

การตรวจสอบความต่างของไฟล์

git diff (commitid)

ดู remote

git remote

ดู remote URL

git remote -v

เพิ่ม remote

  • git remote add ชื่ออ้างถึงGit URLGit
git remote add origin https://github.com/naruepol/javaremotehub.git
git push -u origin master

ตรวจสอบ remote

git remote show origin

การดู tag

git tag

การดู tag ที่มีเวอร์ชันย่อย

git tag -l v1.0.*

ดูเวอร์ชันย่อยๆ ของ v1.0 เช่น v1.0.1, v1.0.2 เป็นต้น

การติด tag

git tag -a v1.0 -m “version 1.0”

การนำ tag ไปยัง remote

git push -u (git-remote-ref) (branch) — tags

ดูรายละเอียดของ tag

git show v1.0

Signed Tag

git tag -s v1.0 -m “Signed 1.0 tag”

Verifying Tags

git tag -v v1.0

Sharing Tag

git push origin v1.0

การแตก branch

git branch testing
git checkout testing

สร้าง branch พร้อมกับการ checkout ไปยัง branch ใหม่

git checkout -b testing

การกรณีการ merge ในลักษณะต่างๆ

  1. การ merge กรณีที่แก้ไขไฟล์ต่างกัน
  2. การ merge กรณีที่แก้ไขไฟล์เดียวกัน

การ merge ไปที่ branch หลักก่อน เช่น master

git checkout master
git merge testing

การ merge พร้อมทั้งลบ branch ที่ merge

ต้อง checkout ไปที่ branch หลักก่อน

git merge --no-ff (feature)

--no-ff flag causes the merge to always create a new commit object, even if the merge could be performed with a fast-forward.

การลบ branch

git branch -d testing

การลบ branch ที่ remote

git push (branch) — delete (remote-ref)

การจัดการ branch

การดู branch

git branch

การดู branch ที่มีข้อความประกอบ

git branch -v

การดู branch ที่มีการ merge

git branch — merged

การดู branch ที่ไม่ได้ merge

git branch — no-merged

การเพิ่ม remote ที่มากกว่า 1

git remote add javahub https://github.com/naruepol/javaremotehub.git
git push -u javahub master
git remote add javalab git@gitlab.com:naruapon/javaproremote.git
git push -u javalab master

การนำ Source Code ไปยังremote

git push (remote) (branch)
git push origin xxx

origin คือ ชื่อของ remote ที่อ้างถึง

xxx คือ branch

หากต้องการ นำทุก branch ไปยัง remote ให้ใช้ — all

git push (remote) (branch)--all

การดึงข้อมูลจากที่อ้างถึง โดยใช้ fetch (ตรวจสอบว่ามีการเปลี่ยนแปลง ก่อนทำการแก้ไข )

git fetch origin master

การดึงข้อมูลจากที่อ้างถึง โดยใช้ pull (ดาวน์โหลดเฉพาะไฟล์ใหม่ใน remote และแก้ปัญหาการ merge)

git pull origin master

รวม รีโมท -> โลคอล อาจจะทำให้เกิด conflict ในการรวม

ลบไฟล์ Untrack ด้วย git clean

ลบไฟล์

git clean -f

ดูว่าไฟล์ใดจะถูกลบบ้างหากใช้คำสั่ง

git clean -n

ย้ายในสิ่งที่กำลังพัฒนา ให้เป็นการทำงานที่ commit ล่าสุด เพื่อทำการแก้ไข แล้วจึงค่อยกลับมาทำ

git stash

ดูรายการ stash

git stash list

การนำเอา stash มาใช้

git stash apply stash@{0}

การนำ stash ออก

git stash drop stash@{0}

การกลับไปยัง stash

git stash pop

การทำ Merge Commit สำหรับการพัฒนาเป็นทีม

  • ทดลองโดยจับคู่ในการพัฒนา

Host Git

  • github
  • gitlab
  • bitbucket

Tool

  • GitHub Desktop
  • Sourcetree

Flow ในการพัฒนาโดยทั่วไป ด้วย Git Flow

  • master
  • develop
  • feature
  • release
  • hotfix

Fork

Fork การคัดลอก Repository ของนักพัฒนาคนอื่นๆ มาไว้ที่ Host ของเรา (ต่างจาก clone ที่นำมาไว้ที่ Local ของเรา) เช่น GitHub

Pull Request

การส่งไปยัง remote Host ที่เราทำการ fork มาเพื่อให้อัพเดทตามที่ เรา commit