กลับไปหน้าสูตร
#github#gh#cli#cheatsheet
GitHub CLI Cheatsheet
รวมคำสั่ง GitHub CLI (gh) แบบจัดเต็ม: auth, repo, branch, PR, issues, releases, actions, api และ automation workflow
11 มีนาคม 2569อ่านประมาณ 5 นาที
สารบัญสูตร
Install & AuthAccount / ProfileRepository BasicsBranch / Local IntegrationPull Requests (Create & Manage)Pull Requests (Review & Merge)Issue ManagementLabels, Milestones, ProjectsReleases & TagsGitHub Actionsgh api (Power User)SearchNotificationsGistsAliases (เพิ่มความเร็ว)ExtensionsRepo Automation RecipesUseful JSON Fields (for --json)TroubleshootingDaily Workflow (Team)
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/repo | clone repo |
gh repo fork --clone | fork และ clone |
gh repo sync owner/repo -b main | sync 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 123 | checkout branch ของ PR |
gh pr ready 123 | mark 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 --approve | approve 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 --merge | merge แบบ merge commit |
gh pr merge 123 --squash | merge แบบ squash |
gh pr merge 123 --rebase | merge แบบ rebase |
gh pr merge 123 --auto | เปิด auto-merge |
gh pr merge 123 --admin | admin merge |
Issue Management
| Command | ใช้ทำอะไร |
|---|---|
gh issue list | ดู issues |
gh issue list --assignee @me | issue ที่ assign ให้เรา |
gh issue list --label bug | filter ตาม 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 |
GitHub Actions
| Command | ใช้ทำอะไร |
|---|---|
gh workflow list | ดู workflows |
gh workflow view ci.yml | ดู workflow |
gh workflow run ci.yml | trigger 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 |
Search
| Command | ใช้ทำอะไร |
|---|---|
gh search repos "topic:nextjs stars:>1000" | ค้น repo |
gh search issues "is:open label:bug repo:owner/repo" | ค้น issues |
gh search prs "author:@me is:open" | ค้น PRs |
gh search code "TODO" --repo owner/repo | ค้นโค้ด |
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)
| Command | Fields แนะนำ |
|---|---|
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 401 | gh 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)
| Step | Command |
|---|---|
| sync main | git switch main && git pull --rebase |
| new branch | git switch -c feature/x |
| push branch | git push -u origin HEAD |
| open PR | gh pr create --fill |
| monitor checks | gh pr checks |
| review others | gh pr list + gh pr review |
| merge | gh pr merge --squash --delete-branch |