กลับไปหน้าสูตร
#github#gh#cli#cheatsheet

GitHub CLI Cheatsheet

รวมคำสั่ง GitHub CLI (gh) แบบจัดเต็ม: auth, repo, branch, PR, issues, releases, actions, api และ automation workflow

11 มีนาคม 2569อ่านประมาณ 5 นาที

Install & Auth

Commandใช้ทำอะไร
gh --versionดูเวอร์ชัน
gh auth loginล็อกอิน GitHub
gh auth statusตรวจสถานะการล็อกอิน
gh auth refresh -h github.com -s repo,read:orgขอ permission เพิ่ม
gh auth logoutออกจากระบบ
gh config set git_protocol sshตั้งค่าใช้ SSH
gh config get editorดูค่า editor
gh config set editor "code --wait"ตั้ง editor

Account / Profile

Commandใช้ทำอะไร
gh api userดูข้อมูล user ปัจจุบัน
gh api user/orgsดู org ที่เป็นสมาชิก
gh ssh-key listดู SSH keys
gh ssh-key add ~/.ssh/id_ed25519.pub -t "macbook"เพิ่ม SSH key

Repository Basics

Commandใช้ทำอะไร
gh repo viewดู repo ปัจจุบัน
gh repo view -wเปิดหน้า repo บนเว็บ
gh repo create my-repo --private --cloneสร้าง repo และ clone
gh repo clone owner/repoclone repo
gh repo fork --clonefork และ clone
gh repo sync owner/repo -b mainsync fork กับ upstream
gh repo edit --visibility privateเปลี่ยน visibility
gh repo rename new-nameเปลี่ยนชื่อ repo
gh repo delete owner/repo --yesลบ repo

Branch / Local Integration

Commandใช้ทำอะไร
gh repo set-default owner/repoตั้ง default repo สำหรับคำสั่ง gh
gh browseเปิด repo ปัจจุบันใน browser
gh browse --branch feature/loginเปิด branch เฉพาะ
gh browse path/to/file.tsเปิดไฟล์ในเว็บ

Pull Requests (Create & Manage)

Commandใช้ทำอะไร
gh pr statusดูสถานะ PR ที่เกี่ยวข้อง
gh pr listดูรายการ PR
gh pr list --state allดู PR ทุกสถานะ
gh pr createสร้าง PR
gh pr create --fillเติม title/body จาก commits
gh pr create --base main --head feature/xระบุ base/head
gh pr view 123ดู PR รายการ
gh pr view 123 -wเปิด PR บนเว็บ
gh pr checkout 123checkout branch ของ PR
gh pr ready 123mark ready for review
gh pr close 123ปิด PR
gh pr reopen 123เปิด PR ใหม่

Pull Requests (Review & Merge)

Commandใช้ทำอะไร
gh pr diff 123ดู diff PR
gh pr checks 123ดู CI checks
gh pr review 123 --approveapprove PR
gh pr review 123 -r -b "please fix"request changes
gh pr review 123 -c -b "comment"comment review
gh pr comment 123 -b "LGTM"คอมเมนต์ใน PR
gh pr merge 123 --mergemerge แบบ merge commit
gh pr merge 123 --squashmerge แบบ squash
gh pr merge 123 --rebasemerge แบบ rebase
gh pr merge 123 --autoเปิด auto-merge
gh pr merge 123 --adminadmin merge

Issue Management

Commandใช้ทำอะไร
gh issue listดู issues
gh issue list --assignee @meissue ที่ assign ให้เรา
gh issue list --label bugfilter ตาม label
gh issue createสร้าง issue
gh issue create --title "Bug..." --body "..."create แบบเร็ว
gh issue view 42ดู issue
gh issue view 42 -wเปิด issue บนเว็บ
gh issue comment 42 -b "working on this"คอมเมนต์ issue
gh issue close 42ปิด issue
gh issue reopen 42เปิด issue ใหม่
gh issue transfer 42 owner/another-repoย้าย issue

Labels, Milestones, Projects

Commandใช้ทำอะไร
gh label listดู labels
gh label create "priority:high" --color FF0000สร้าง label
gh label delete "wontfix"ลบ label
gh milestone listดู milestones
gh project list --owner @meดู projects
gh project item-add <project-id> --url <issue-or-pr-url>เพิ่ม item เข้า project

Releases & Tags

Commandใช้ทำอะไร
gh release listดู releases
gh release view v1.2.0ดู release
gh release create v1.2.0สร้าง release
gh release create v1.2.0 --generate-notesauto notes
gh release create v1.2.0 dist/* --title "v1.2.0"แนบ asset
gh release upload v1.2.0 build.zipอัปโหลด asset เพิ่ม
gh release delete v1.2.0 --yesลบ release
gh release download v1.2.0 -D ./downloadsดาวน์โหลด assets

GitHub Actions

Commandใช้ทำอะไร
gh workflow listดู workflows
gh workflow view ci.ymlดู workflow
gh workflow run ci.ymltrigger workflow
gh run listดู workflow runs
gh run view <run-id>ดูรายละเอียด run
gh run view <run-id> --logดู logs
gh run download <run-id>ดาวน์โหลด artifacts
gh run rerun <run-id>rerun run
gh run cancel <run-id>ยกเลิก run

gh api (Power User)

Commandใช้ทำอะไร
gh api repos/{owner}/{repo}เรียก REST API
gh api repos/{owner}/{repo}/pulls --jq '.[].title'ใช้ jq filter
gh api graphql -f query='query { viewer { login } }'เรียก GraphQL
gh api -X POST repos/{owner}/{repo}/issues -f title='Bug'POST ผ่าน API
gh api rate_limitดู API rate limits

Notifications

Commandใช้ทำอะไร
gh notification listดู notifications
gh notification statusดูสถานะ unread
gh notification view <id>ดู notification รายการ

Gists

Commandใช้ทำอะไร
gh gist listดู gists
gh gist create file.txt -d "notes"สร้าง gist
gh gist view <id> -wเปิด gist บนเว็บ
gh gist edit <id>แก้ gist
gh gist delete <id>ลบ gist

Aliases (เพิ่มความเร็ว)

Commandใช้ทำอะไร
gh alias set co 'pr checkout'alias checkout PR
gh alias set pv 'pr view'alias view PR
gh alias set pl 'pr list --limit 20'alias PR list
gh alias listดู aliases
gh alias delete coลบ alias

Extensions

Commandใช้ทำอะไร
gh extension listดู extensions
gh extension install owner/gh-ext-nameติดตั้ง extension
gh extension upgrade --allอัปเดต extension ทั้งหมด
gh extension remove gh-ext-nameลบ extension

Repo Automation Recipes

1) สร้าง PR จาก branch ปัจจุบันแบบเร็ว

git push -u origin HEAD
gh pr create --fill --base main --head "$(git branch --show-current)"

2) เช็ก PR ที่ CI พัง

gh pr list --state open --json number,title | jq -r '.[] | "\(.number) \(.title)"'
# แล้วเช็กทีละตัว
gh pr checks <number>

3) Trigger workflow deploy และดู log

gh workflow run deploy.yml -f env=staging
gh run list --limit 5
gh run view <run-id> --log

4) ปิด issue พร้อมคอมเมนต์

gh issue comment 123 -b "fixed in #456"
gh issue close 123

Useful JSON Fields (for --json)

CommandFields แนะนำ
gh pr list --json ...number,title,author,reviewDecision,mergeable,headRefName
gh issue list --json ...number,title,labels,assignees,createdAt,updatedAt
gh run list --json ...databaseId,displayTitle,workflowName,status,conclusion,headBranch

Troubleshooting

ปัญหาวิธีแก้
HTTP 401gh auth login ใหม่ / เช็ก token scopes
Resource not accessible by integrationเพิ่ม scope ที่ต้องใช้ (repo, workflow)
not a git repositoryเข้า directory ที่เป็น git repo ก่อน
merge ไม่ได้เช็ก required checks/protection rules
workflow run ไม่ขึ้นเช็กชื่อ workflow file และ branch

Daily Workflow (Team)

StepCommand
sync maingit switch main && git pull --rebase
new branchgit switch -c feature/x
push branchgit push -u origin HEAD
open PRgh pr create --fill
monitor checksgh pr checks
review othersgh pr list + gh pr review
mergegh pr merge --squash --delete-branch