Cách Sử Dụng Github CLI

Dùng gh command-line tool để làm việc với GitHub nhanh hơn

GitHub CLI (gh) cho phép làm việc với GitHub ngay từ terminal. Không cần mở browser -- tạo PRs, check issues, quản lý repos mà không rời khỏi nơi mình đang code.

Ý tưởng lõi: Ở trong flow. Terminal là nơi mình code, nên cũng là nơi quản lý code.

Vì sao dùng CLI

TaskBrowserCLI
Tạo PRMở browser → Tìm repo → Click nút → Điền formgh pr create
Check trạng thái PRMở browser → Tìm PR → Đọcgh pr status
Xem issueMở browser → Vào issuesgh issue view 123

Từng giây cộng dồn. Quan trọng hơn, giữ được sự tập trung -- không bị browser hút vào mấy tab khác.

Cài đặt

# macOS
brew install gh

# Windows (Chocolatey)
choco install gh

# Windows (Winget)
winget install GitHub.cli

Sau khi cài, xác thực:

gh auth login

Làm theo hướng dẫn để kết nối tài khoản GitHub.

Các Lệnh Thiết Yếu

Pull Requests

# Tạo PR (interactive)
gh pr create

# Tạo PR với title và body
gh pr create --title "Add keyword export" --body "Exports results to CSV"

# Tạo PR tự điền từ commit messages
gh pr create --fill

# Xem PRs của mình
gh pr status

# Xem một PR cụ thể
gh pr view 123

# Checkout PR về máy để test
gh pr checkout 123

# Merge PR
gh pr merge 123

# Đóng PR
gh pr close 123

Issues

# Tạo issue (interactive)
gh issue create

# Tạo issue với title
gh issue create --title "Bug: Export fails for large datasets"

# Liệt kê issues đang mở
gh issue list

# Xem một issue
gh issue view 123

# Đóng issue
gh issue close 123

# Mở lại issue
gh issue reopen 123

Repository

# Clone repo
gh repo clone owner/repo

# Tạo repo mới
gh repo create my-project

# Mở repo trên browser
gh repo view --web

# Fork repo
gh repo fork owner/repo

Workflow (GitHub Actions)

# Liệt kê workflow runs
gh run list

# Xem một run cụ thể
gh run view 123

# Theo dõi run đang chạy
gh run watch

# Chạy lại workflow bị lỗi
gh run rerun 123

Workflow Hàng Ngày

Bắt đầu feature

# Lấy main mới nhất
git checkout main && git pull

# Tạo feature branch
git checkout -b feature/keyword-export

# ... viết code ...

# Push và tạo PR trong một bước
git push -u origin feature/keyword-export
gh pr create --fill

Review PR

# Xem cái gì cần review
gh pr status

# Checkout PR về máy để test
gh pr checkout 123

# Test xong, approve
gh pr review 123 --approve

# Hoặc request changes
gh pr review 123 --request-changes --body "Need to handle empty datasets"

Xử lý issue nhanh

# Xem issues đang mở
gh issue list

# Thêm label
gh issue edit 123 --add-label "bug"

# Assign cho mình
gh issue edit 123 --add-assignee @me

# Đóng kèm comment
gh issue close 123 --comment "Fixed in PR #456"

Aliases

Thêm vào shell config để nhanh hơn nữa:

# Tạo PR nhanh
alias gpr="gh pr create --fill"

# Check trạng thái PR
alias gprs="gh pr status"

# Liệt kê PRs của mình
alias gprl="gh pr list --author @me"

# Mở PR hiện tại trên browser
alias gprv="gh pr view --web"

CLI hay Browser?

CLIBrowser
Tạo PRsReview PR phức tạp với nhiều files
Check trạng thái nhanhĐọc thảo luận dài
Thao tác routineQuản lý project boards
Scripting/automationSetup repo lần đầu

Tham Khảo Nhanh

Mình muốn...Lệnh
Tạo PRgh pr create
Xem PRs của mìnhgh pr status
Merge PRgh pr merge 123
Tạo issuegh issue create
Liệt kê issuesgh issue list
Clone repogh repo clone owner/repo
Xem workflow runsgh run list
Mở repo trên browsergh repo view --web

Tài Liệu Tham Khảo