เอกสารอ้างอิง Git ที่ค้นหาและพิมพ์ได้——การตั้งค่า, branch, การ merge, การ rebase, remote, การ stash, tag และการย้อนการเปลี่ยนแปลง ฟรี

ตั้งค่าและกำหนดค่า

10
git config --global user.name "Jane Doe"
ตั้งชื่อที่แนบกับ commit
git config --global user.email "jane@example.com"
ตั้งอีเมลที่แนบกับ commit
git config --global init.defaultBranch main
ให้ repo ใหม่เริ่มที่ branch "main"
git config --global core.editor "code --wait"
ใช้ VS Code เป็น editor เริ่มต้นของ Git
git config --global pull.rebase true
ใช้ rebase แทน merge เมื่อ pull
git config --global alias.co checkout
สร้าง alias ทางลัด (git co)
git config --global --list
แสดงค่าคอนฟิก global ทั้งหมด
git config user.email
แสดงอีเมลของ repo ปัจจุบัน
git config --global color.ui auto
เปิดเอาต์พุตสีบน command-line
git help <command>
เปิดหน้าคู่มือของคำสั่ง

สร้างและ clone repo

9
git init
สร้าง repo ใหม่ในโฟลเดอร์ปัจจุบัน
git init my-project
สร้าง repo ใหม่ในไดเรกทอรีใหม่
git clone https://example.com/repo.git
Clone repo ระยะไกลผ่าน HTTPS
git clone git@example.com:user/repo.git
Clone repo ระยะไกลผ่าน SSH
git clone <url> my-dir
Clone ลงในชื่อไดเรกทอรีที่กำหนด
git clone --depth 1 <url>
Shallow clone เฉพาะ commit ล่าสุด
git clone --branch dev <url>
Clone และ checkout branch ที่กำหนด
git clone --recurse-submodules <url>
Clone และเริ่ม submodule ทั้งหมด
git remote add origin <url>
เพิ่ม remote ให้ repo local ที่มีอยู่

Staging และ committing

11
git add file.txt
Stage ไฟล์เดียวสำหรับ commit ถัดไป
git add .
Stage การเปลี่ยนแปลงทั้งหมดในไดเรกทอรีปัจจุบัน
git add -A
Stage การเปลี่ยนแปลงทั้งหมดรวมการลบ
git add -p
Stage hunk ที่เลือกแบบโต้ตอบ
git commit -m "Add login form"
Commit การเปลี่ยนแปลงที่ stage พร้อมข้อความ
git commit -am "Fix typo"
Stage ไฟล์ที่ติดตามและ commit ในขั้นตอนเดียว
git commit --amend
แก้ไข commit ล่าสุด
git commit --amend --no-edit
เพิ่มการเปลี่ยนแปลงที่ stage เข้า commit ล่าสุด
git rm file.txt
ลบไฟล์และ stage การลบ
git mv old.txt new.txt
เปลี่ยนชื่อหรือย้ายไฟล์และ stage
git reset file.txt
Unstage ไฟล์แต่คงการเปลี่ยนแปลงไว้

Branch

10
git branch
แสดง branch local ทั้งหมด
git branch -a
แสดง branch local และ remote-tracking
git switch -c feature/login
สร้าง branch ใหม่และสลับไป
git switch main
สลับไป branch ที่มีอยู่
git checkout -b hotfix
วิธีดั้งเดิมในการสร้างและสลับ branch
git branch -m old-name new-name
เปลี่ยนชื่อ branch
git branch -d feature/login
ลบ branch local ที่ merge แล้ว
git branch -D feature/login
บังคับลบ branch local ที่ยังไม่ merge
git push origin --delete feature/login
ลบ branch บน remote
git switch -
สลับกลับไป branch ก่อนหน้า

Merge และ rebase

10
git merge feature/login
Merge branch เข้า branch ปัจจุบัน
git merge --no-ff feature/login
Merge และสร้าง merge commit เสมอ
git merge --squash feature/login
รวม branch เป็นการเปลี่ยนแปลง stage เดียว
git merge --abort
ยกเลิก merge ที่มี conflict
git rebase main
เล่นซ้ำ commit ของ branch ปัจจุบันลงบน main
git rebase -i HEAD~3
แก้ไข 3 commit ล่าสุดแบบโต้ตอบ
git rebase --continue
ทำ rebase ต่อหลังแก้ conflict
git rebase --abort
ยกเลิก rebase ที่กำลังทำ
git cherry-pick <hash>
นำ commit เดียวมาใส่ branch ปัจจุบัน
git mergetool
เปิดเครื่องมือแก้ merge conflict

Remote และการ sync

11
git remote -v
แสดง remote ที่ตั้งค่าและ URL
git remote add upstream <url>
เพิ่ม remote ที่สองชื่อ upstream
git remote set-url origin <url>
เปลี่ยน URL ของ remote ที่มีอยู่
git fetch
ดาวน์โหลดการเปลี่ยนแปลงจาก remote โดยไม่ merge
git fetch --all --prune
Fetch ทุก remote และลบ branch ที่ค้าง
git pull
Fetch และรวมการเปลี่ยนแปลงจาก remote
git pull --rebase
Pull และ rebase commit local ไว้ด้านบน
git push
อัปโหลด commit local ไปยัง remote
git push -u origin main
Push และตั้ง branch upstream tracking
git push --force-with-lease
Force-push อย่างปลอดภัยโดยไม่ทับงานคนอื่น
git push origin --tags
Push tag local ทั้งหมดไป remote

ตรวจสอบและเปรียบเทียบ

10
git status
แสดงไฟล์ staged, unstaged และ untracked
git status -s
แสดงสถานะแบบสั้นกระชับ
git diff
แสดงการเปลี่ยนแปลง unstaged เทียบกับ index
git diff --staged
แสดงการเปลี่ยนแปลงที่ stage สำหรับ commit ถัดไป
git diff main..feature
เปรียบเทียบสอง branch
git diff HEAD~1 HEAD
เปรียบเทียบ commit ล่าสุดกับ parent
git show <hash>
แสดงรายละเอียดและ diff ของ commit เดียว
git show HEAD:file.txt
แสดงไฟล์ตามที่อยู่ใน commit
git log --stat
แสดง commit พร้อมสรุปไฟล์ที่เปลี่ยน
git shortlog -sn
นับ commit จัดกลุ่มตามผู้เขียน

ยกเลิกการเปลี่ยนแปลง

10
git restore file.txt
ทิ้งการเปลี่ยนแปลง unstaged ในไฟล์
git restore --staged file.txt
Unstage ไฟล์แต่คงการเปลี่ยนแปลงไว้
git restore --source=HEAD~1 file.txt
กู้คืนไฟล์จาก commit ก่อนหน้า
git checkout -- file.txt
วิธีดั้งเดิมในการทิ้งการเปลี่ยนแปลง local
git reset --soft HEAD~1
ยกเลิก commit ล่าสุด คงการเปลี่ยนแปลง stage ไว้
git reset --mixed HEAD~1
ยกเลิก commit ล่าสุด คงการเปลี่ยนแปลง unstage ไว้
git reset --hard HEAD~1
ยกเลิก commit ล่าสุดและทิ้งการเปลี่ยนแปลง
git revert <hash>
สร้าง commit ใหม่ที่ย้อน commit
git clean -fd
ลบไฟล์และไดเรกทอรีที่ untracked
git clean -nd
ดูตัวอย่างสิ่งที่ clean จะลบ

Stashing

10
git stash
บันทึกการเปลี่ยนแปลงที่ยังไม่ commit และล้าง tree
git stash push -m "wip"
Stash การเปลี่ยนแปลงพร้อมข้อความอธิบาย
git stash -u
Stash รวมไฟล์ที่ untracked
git stash list
แสดงชุดการเปลี่ยนแปลงที่ stash ทั้งหมด
git stash show -p
แสดง diff ของ stash ล่าสุด
git stash apply
นำ stash ล่าสุดมาใช้และเก็บไว้
git stash pop
นำ stash ล่าสุดมาใช้และลบทิ้ง
git stash apply stash@{2}
นำ stash ที่กำหนดมาใช้ตาม index
git stash drop stash@{0}
ลบ stash รายการเดียว
git stash clear
ลบ stash ทุกรายการ

Tag

9
git tag
แสดง tag ทั้งหมด
git tag v1.0.0
สร้าง lightweight tag ที่ HEAD
git tag -a v1.0.0 -m "Release 1.0.0"
สร้าง annotated tag พร้อมข้อความ
git tag -a v1.0.0 <hash>
Tag commit ในอดีตที่กำหนด
git show v1.0.0
แสดงรายละเอียดของ tag
git push origin v1.0.0
Push tag เดียวไป remote
git push origin --tags
Push tag local ทั้งหมดไป remote
git tag -d v1.0.0
ลบ tag แบบ local
git push origin --delete v1.0.0
ลบ tag บน remote

Log และประวัติ

12
git log
แสดงประวัติ commit ทั้งหมด
git log --oneline
แสดง log แบบหนึ่งบรรทัดต่อ commit
git log --oneline --graph --all
แสดง branch เป็นกราฟ ASCII
git log -p
แสดงประวัติ commit พร้อม diff
git log --author="Jane"
กรองประวัติตามผู้เขียน
git log --since="2 weeks ago"
กรองประวัติตามช่วงวันที่
git log --pretty=format:"%h %an %s"
ปรับแต่งรูปแบบเอาต์พุตของ log
git blame file.txt
แสดงว่าใครแก้แต่ละบรรทัดล่าสุด
git reflog
แสดงประวัติตำแหน่งที่ HEAD เคยอยู่
git bisect start
เริ่มค้นหาแบบ binary หา commit ที่เสีย
git bisect good <hash>
ทำเครื่องหมาย commit ว่าดีระหว่าง bisect
git bisect bad
ทำเครื่องหมาย commit ปัจจุบันว่าเสีย

ไม่มีรายการที่ตรงกับ “:q”


แชร์สิ่งนี้
ต้องการความช่วยเหลือ?
พบปัญหากับเครื่องมือนี้หรือไม่? แจ้งทีมงานของเรา
รายงานปัญหา

เพิ่มเครื่องมือฟรีนี้ลงในเว็บไซต์ของคุณเอง — คัดลอกและวางโค้ดด้านล่าง