Cài Đặt
Cài Đặt
brew install ghmacOS qua Homebrew
sudo apt install ghDebian / Ubuntu
winget install GitHub.cliWindows qua winget
conda install ghQua conda-forge
Xác Thực
gh auth login # interactive login gh auth login --with-token < token.txt gh auth status # check auth state gh auth refresh -s repo,gist # add scopes
Cấu Hình
gh config set editor vim gh config set pager less gh config set git_protocol ssh gh config list
Repos
Lệnh Repository
gh repo create my-app --public --clone gh repo clone owner/repo gh repo fork owner/repo --clone gh repo view owner/repo --web
Tùy Chọn Repo
--public | --privateThiết lập khả năng hiển thị repository
--template owner/repoTạo từ template repo
--cloneClone sau khi tạo
--add-readmeKhởi tạo với README
gh repo list ownerLiệt kê repos cho owner
gh repo delete owner/repoXóa repository (có xác nhận)
gh repo rename new-nameĐổi tên repo hiện tại
gh repo archive owner/repoLưu trữ repository
Issues
Quản Lý Issues
gh issue create --title "Bug" --body "Details here" gh issue list --state open --label bug gh issue view 42 gh issue close 42 --reason completed
Tùy Chọn Issue
--assignee @meGiao cho bản thân
--label bug,urgentThêm nhãn
--milestone v2.0Thiết lập milestone
--project "Board"Thêm vào project
gh issue edit 42Chỉnh sửa issue tương tác
gh issue reopen 42Mở lại issue đã đóng
gh issue comment 42 -b "msg"Thêm bình luận vào issue
gh issue pin 42Ghim issue vào repo
Pull Requests
Tạo & Quản Lý PRs
gh pr create --title "feat: add auth" --body "..." gh pr create --fill # title/body from commits gh pr list --state open gh pr view 123 --web
Review & Merge
gh pr checkout 123 # check out PR branch gh pr diff 123 # view PR diff gh pr review 123 --approve gh pr merge 123 --squash --delete-branch
Tùy Chọn PR
--draftTạo PR dạng draft
--reviewer user1,user2Yêu cầu reviewer
--base mainThiết lập base branch
--merge | --squash | --rebaseChiến lược merge
--autoBật auto-merge khi checks pass
--delete-branchXóa branch sau khi merge
gh pr ready 123Đánh dấu draft PR là sẵn sàng
Actions
Lệnh Workflow
gh run list # recent runs gh run view 12345 # run details gh run view 12345 --log-failed # failed step logs gh run watch 12345 # live status
Kích Hoạt & Quản Lý
gh workflow run deploy.yml --ref main gh workflow list gh workflow view deploy.yml gh run rerun 12345 --failed # rerun failed jobs
Tùy Chọn Actions
-f key=valueTruyền input cho workflow_dispatch
--jsonOutput dạng JSON
-b branchLọc theo branch
gh run download 12345Tải xuống artifacts của run
gh cache listLiệt kê Actions caches
gh cache delete KEYXóa một cache entry
Releases
Quản Lý Releases
gh release create v1.0.0 --generate-notes gh release create v1.0.0 ./dist/*.tar.gz gh release list gh release view v1.0.0
Tùy Chọn Release
--title "Release v1.0"Thiết lập tiêu đề release
--notes "Changelog here"Thiết lập ghi chú release trực tiếp
--notes-file CHANGELOG.mdGhi chú từ file
--generate-notesTự động tạo từ commits
--draftTạo dạng draft
--prereleaseĐánh dấu là prerelease
--latestĐánh dấu là release mới nhất
gh release download v1.0.0Tải xuống release assets
gh release delete v1.0.0Xóa một release
gh release edit v1.0.0Chỉnh sửa metadata release
Gists
Lệnh Gist
gh gist create file.py -d "My snippet" gh gist create file1.js file2.js # multi-file gist gh gist list gh gist view
Tùy Chọn Gist
-d "description"Thiết lập mô tả gist
--publicTạo gist công khai (mặc định: bí mật)
--webMở gist trong trình duyệt
gh gist edit <id>Chỉnh sửa file gist
gh gist clone <id>Clone gist về máy
gh gist delete <id>Xóa gist
API
Gọi API
gh api repos/owner/repo gh api repos/owner/repo/issues --method POST \ -f title="Bug" -f body="Details" gh api graphql -f query='{ viewer { login } }'
Định Dạng Output
gh api repos/owner/repo --jq '.stargazers_count' gh api repos/owner/repo --template '{{.full_name}}' gh pr list --json number,title --jq '.[].title'
Tùy Chọn API
--method GET|POST|PUT|DELETEPhương thức HTTP
-f key=valueThiết lập trường chuỗi
-F key=@fileThiết lập trường từ file
--jq 'expression'Lọc JSON bằng cú pháp jq
--template 'tmpl'Định dạng bằng Go template
--paginateLấy tất cả trang
-H 'Accept: ...'Thiết lập header tùy chỉnh
Aliases
Quản Lý Aliases
gh alias set co 'pr checkout' gh alias set bugs 'issue list --label bug' gh alias set last 'run list -L 1' gh alias list
Aliases Nâng Cao
# Alias with shell command gh alias set --shell pv 'gh pr view --json url --jq .url | pbcopy' # Delete alias gh alias delete co
Mẹo Alias
gh alias set name 'cmd'Tạo alias đơn giản
--shellAlias chạy qua shell (hỗ trợ pipe)
gh alias listHiển thị tất cả aliases đã định nghĩa
gh alias delete nameXóa một alias
Mẫu Phổ Biến
Quy Trình Hàng Ngày
gh issue list --assignee @me # my open issues gh pr status # PRs needing attention gh pr checks 123 # CI status for PR gh run list -b main -L 5 # recent CI runs
Tìm Kiếm
gh search repos --language rust --stars ">1000" gh search issues --repo owner/repo "memory leak" gh search prs --state open --review required
Cờ Hữu Ích
--json field1,field2Output các trường cụ thể dạng JSON
--jq 'expr'Lọc JSON output
-L NGiới hạn kết quả N mục
--webMở kết quả trong trình duyệt
-R owner/repoNhắm mục tiêu repository cụ thể
GH_TOKEN=xxxXác thực qua biến môi trường