{/* Trang này được tạo tự động từ SKILL.md của kỹ năng bởi website/scripts/generate-skill-docs.py. Chỉnh sửa nguồn SKILL.md, không phải trang này. */}
Quản lý kho lưu trữ GitHub
Sao chép/tạo/ngã ba kho lưu trữ; quản lý điều khiển từ xa, phát hành.
Siêu dữ liệu kỹ năng
| Nguồn | Đi kèm (được cài đặt theo mặc định) |
| Đường dẫn |
skills/GitHub/GitHub-repo-management ` | | Phiên bản |
1.1.0 ` | | Tác giả | Đại lý Hermes | | Giấy phép | MIT | | Nền tảng | Linux, macOS, Windows | | Thẻ |
GitHub
, `Repositories
, `Git
, `Releases
, `Secrets
,
Configuration |
| Kỹ năng liên quan | XPROTECTX34XPROTECTX, XPROTECTX35XPROTECTX, XPROTECTX36XPROTECTX |
Tham khảo: đầy đủ SKILL.md
Sau đây là định nghĩa kỹ năng đầy đủ mà Hermes tải khi kỹ năng này được kích hoạt. Đây là những gì tác nhân coi là hướng dẫn khi kỹ năng được kích hoạt.
#Quản lý kho lưu trữ GitHub
Tạo, sao chép, phân nhánh, định cấu hình và quản lý kho lưu trữ GitHub. Mỗi phần hiển thị
gh trước tiên, sau đó là dự phòng
`git
`curl
.
Điều kiện tiên quyết
- Đã xác thực bằng GitHub (xem kỹ năng `GitHub-auth
)
Thiết lập
if command -v gh &>/dev/null && gh auth status &>/dev/null; then
AUTH="gh"
else
AUTH="git"
if [ -z "$GitHub_TOKEN" ]; then
if [ -f ~/.Hermes/.env ] && grep -q "^GitHub_TOKEN=" ~/.Hermes/.env; then
GitHub_TOKEN=$(grep "^GitHub_TOKEN=" ~/.Hermes/.env | head -1 | cut -d= -f2 | tr -d '\n\r')
elif grep -q "GitHub.com" ~/.git-credentials 2>/dev/null; then
GitHub_TOKEN=$(grep "GitHub.com" ~/.git-credentials 2>/dev/null | head -1 | sed 's|https://[^:]*:\([^@]*\)@.*|\1|')
fi
fi
fi
# Get your GitHub username (needed for several operations)
if [ "$AUTH" = "gh" ]; then
GH_USER=$(gh API user --jq '.login')
else
GH_USER=$(curl -s -H "Authorization: token $GitHub_TOKEN" https://API.GitHub.com/user | Python3 -c "import sys,JSON; print(JSON.load(sys.stdin)['login'])")
fi
`
``Nếu bạn đã ở trong một repo:
`bash
REMOTE_URL=$(git remote get-url origin)
OWNER_REPO=$(echo "$REMOTE_URL" | sed -E 's|.*GitHub\.com[:/]||; s|\.git$||')
OWNER=$(echo "$OWNER_REPO" | cut -d/ -f1)
REPO=$(echo "$OWNER_REPO" | cut -d/ -f2)
`
---
## 1. Nhân bản kho lưu trữ
Nhân bản hoàn toàn là
`git
- hoạt động giống hệt nhau:
`bash
# Clone via HTTPS (works with credential helper or token-embedded URL)
git clone https://GitHub.com/owner/repo-name.git
# Clone into a specific directory
git clone https://GitHub.com/owner/repo-name.git ./my-local-dir
# Shallow clone (faster for large repos)
git clone --depth 1 https://GitHub.com/owner/repo-name.git
# Clone a specific branch
git clone --branch develop https://GitHub.com/owner/repo-name.git
# Clone via SSH (if SSH is configured)
git clone git@GitHub.com:owner/repo-name.git
`
``**Với gh (viết tắt):**
``` bash
gh repo clone owner/repo-name
gh repo clone owner/repo-name -- --depth 1
`
## 2. Tạo kho lưu trữ`**Với gh:**
`bash
# Create a public repo and clone it
gh repo create my-new-project --public --clone
# Private, with description and license
gh repo create my-new-project --private --description "A useful tool" --license MIT --clone
# Under an organization
gh repo create my-org/my-new-project --public --clone
# From existing local directory
cd /path/to/existing/project
gh repo create my-project --source . --public --push
`
``**Với git + cuộn tròn:**
``` bash
# Create the remote repo via API
curl -s -X POST \
-H "Authorization: token $GitHub_TOKEN" \
https://API.GitHub.com/user/repos \
-d '{
"name": "my-new-project",
"description": "A useful tool",
"private": false,
"auto_init": true,
"license_template": "mit"
}'
# Clone it
git clone https://GitHub.com/$GH_USER/my-new-project.git
cd my-new-project
# -- OR -- push an existing local directory to the new repo
cd /path/to/existing/project
git init
git add .
git commit -m "Initial commit"
git remote add origin https://GitHub.com/$GH_USER/my-new-project.git
git push -u origin main
`
``Để tạo trong một tổ chức:
``` bash
curl -s -X POST \
-H "Authorization: token $GitHub_TOKEN" \
https://API.GitHub.com/orgs/my-org/repos \
-d '\{"name": "my-new-project", "private": false}'
`
### Từ một mẫu`**Với gh:**
``` bash
gh repo create my-new-app --template owner/template-repo --public --clone
`
``**Với độ cong:**
`bash
curl -s -X POST \
-H "Authorization: token $GitHub_TOKEN" \
https://API.GitHub.com/repos/owner/template-repo/generate \
-d '\{"owner": "'"$GH_USER"'", "name": "my-new-app", "private": false}'
`
## 3. Kho lưu trữ phân nhánh`**Với gh:**
``` bash
gh repo fork owner/repo-name --clone
`
``**Với git + cuộn tròn:**
`bash
# Create the fork via API
curl -s -X POST \
-H "Authorization: token $GitHub_TOKEN" \
https://API.GitHub.com/repos/owner/repo-name/forks
# Wait a moment for GitHub to create it, then clone
sleep 3
git clone https://GitHub.com/$GH_USER/repo-name.git
cd repo-name
# Add the original repo as "upstream" remote
git remote add upstream https://GitHub.com/owner/repo-name.git
`
### Giữ một Fork luôn đồng bộ
``` bash
# Pure git — works everywhere
git fetch upstream
git checkout main
git merge upstream/main
git push origin main
`
``**Với gh (phím tắt):**
``` bash
gh repo sync $GH_USER/repo-name
`
## 4. Thông tin kho lưu trữ`**Với gh:**
`bash
gh repo view owner/repo-name
gh repo list --limit 20
gh search repos "machine learning" --language Python --sort stars
`
``**Với độ cong:**
`bash
# View repo details
curl -s \
-H "Authorization: token $GitHub_TOKEN" \
https://API.GitHub.com/repos/$OWNER/$REPO \
| Python3 -c "
import sys, JSON
r = JSON.load(sys.stdin)
print(f\"Name: \{r['full_name']}\")
print(f\"Description: \{r['description']}\")
print(f\"Stars: \{r['stargazers_count']} Forks: \{r['forks_count']}\")
print(f\"Default branch: \{r['default_branch']}\")
print(f\"Language: \{r['language']}\")"
# List your repos
curl -s \
-H "Authorization: token $GitHub_TOKEN" \
"https://API.GitHub.com/user/repos?per_page=20&sort=updated" \
| Python3 -c "
import sys, JSON
for r in JSON.load(sys.stdin):
vis = 'private' if r['private'] else 'public'
print(f\" \{r['full_name']:40} \{vis:8} \{r.get('language', ''):10} ★\{r['stargazers_count']}\")"
# Search repos
curl -s \
"https://API.GitHub.com/search/repositories?q=machine+learning+language:Python&sort=stars&per_page=10" \
| Python3 -c "
import sys, JSON
for r in JSON.load(sys.stdin)['items']:
print(f\" \{r['full_name']:40} ★\{r['stargazers_count']:6} \{r['description'][:60] if r['description'] else ''}\")"
`
## 5. Cài đặt kho lưu trữ`**Với gh:**
``` bash
gh repo edit --description "Updated description" --visibility public
gh repo edit --enable-wiki=false --enable-issues=true
gh repo edit --default-branch main
gh repo edit --add-topic "machine-learning,Python"
gh repo edit --enable-auto-merge
`
``**Với độ cong:**
`bash
curl -s -X PATCH \
-H "Authorization: token $GitHub_TOKEN" \
https://API.GitHub.com/repos/$OWNER/$REPO \
-d '{
"description": "Updated description",
"has_wiki": false,
"has_issues": true,
"allow_auto_merge": true
}'
# Update topics
curl -s -X PUT \
-H "Authorization: token $GitHub_TOKEN" \
-H "Accept: application/vnd.GitHub.mercy-preview+JSON" \
https://API.GitHub.com/repos/$OWNER/$REPO/topics \
-d '\{"names": ["machine-learning", "Python", "automation"]}'
`
## 6. Bảo vệ chi nhánh
``` bash
# View current protection
curl -s \
-H "Authorization: token $GitHub_TOKEN" \
https://API.GitHub.com/repos/$OWNER/$REPO/branches/main/protection
# Set up branch protection
curl -s -X PUT \
-H "Authorization: token $GitHub_TOKEN" \
https://API.GitHub.com/repos/$OWNER/$REPO/branches/main/protection \
-d '{
"required_status_checks": {
"strict": true,
"contexts": ["ci/test", "ci/lint"]
},
"enforce_admins": false,
"required_pull_request_reviews": {
"required_approving_review_count": 1
},
"restrictions": null
}'
`
## 7. Quản lý bí mật (Tác vụ GitHub)
**Với gh:**
``` bash
gh secret set API_KEY --body "your-secret-value"
gh secret set SSH_KEY < ~/.SSH/id_rsa
gh secret list
gh secret delete API_KEY
`
``**Với độ cong:**
Bí mật yêu cầu mã hóa bằng khóa chung của repo - liên quan nhiều hơn thông qua API:
`bash
# Get the repo's public key for encrypting secrets
curl -s \
-H "Authorization: token $GitHub_TOKEN" \
https://API.GitHub.com/repos/$OWNER/$REPO/actions/secrets/public-key
# Encrypt and set (requires Python with PyNaCl)
Python3 -c "
from base64 import b64encode
from nacl import encoding, public
import JSON, sys
# Get the public key
key_id = '<key_id_from_above'
public_key = '<base64_key_from_above'
# Encrypt
sealed = public.SealedBox(
public.PublicKey(public_key.encode('utf-8'), encoding.Base64Encoder)
).encrypt('your-secret-value'.encode('utf-8'))
print(JSON.dumps({
'encrypted_value': b64encode(sealed).decode('utf-8'),
'key_id': key_id
}))"
# Then PUT the encrypted secret
curl -s -X PUT \
-H "Authorization: token $GitHub_TOKEN" \
https://API.GitHub.com/repos/$OWNER/$REPO/actions/secrets/API_KEY \
-d '<output from Python script above'
# List secrets (names only, values hidden)
curl -s \
-H "Authorization: token $GitHub_TOKEN" \
https://API.GitHub.com/repos/$OWNER/$REPO/actions/secrets \
| Python3 -c "
import sys, JSON
for s in JSON.load(sys.stdin)['secrets']:
print(f\" \{s['name']:30} updated: \{s['updated_at']}\")"
`
``Lưu ý: Đối với bí mật,
`gh secret set
` đơn giản hơn đáng kể. Nếu cần cài đặt bí mật và
`gh
` không khả dụng, bạn nên cài đặt nó cho thao tác đó.
## 8. Phát hành`**Với gh:**
``` bash
gh release create v1.0.0 --title "v1.0.0" --generate-notes
gh release create v2.0.0-rc1 --draft --prerelease --generate-notes
gh release create v1.0.0 ./dist/binary --title "v1.0.0" --notes "Release notes"
gh release list
gh release download v1.0.0 --dir ./downloads
`
``**Với độ cong:**
`bash
# Create a release
curl -s -X POST \
-H "Authorization: token $GitHub_TOKEN" \
https://API.GitHub.com/repos/$OWNER/$REPO/releases \
-d '{
"tag_name": "v1.0.0",
"name": "v1.0.0",
"body": "## Changelog\n- Feature A\n- Bug fix B",
"draft": false,
"prerelease": false,
"generate_release_notes": true
}'
# List releases
curl -s \
-H "Authorization: token $GitHub_TOKEN" \
https://API.GitHub.com/repos/$OWNER/$REPO/releases \
| Python3 -c "
import sys, JSON
for r in JSON.load(sys.stdin):
tag = r.get('tag_name', 'no tag')
print(f\" \{tag:15} \{r['name']:30} \{'draft' if r['draft'] else 'published'}\")"
# Upload a release asset (binary file)
RELEASE_ID=<id_from_create_response
curl -s -X POST \
-H "Authorization: token $GitHub_TOKEN" \
-H "Content-Type: application/octet-stream" \
"https://uploads.GitHub.com/repos/$OWNER/$REPO/releases/$RELEASE_ID/assets?name=binary-amd64" \
--data-binary @./dist/binary-amd64
`
## 9. Quy trình làm việc của các hành động trên GitHub`**Với gh:**
``` bash
gh workflow list
gh run list --limit 10
gh run view <RUN_ID
gh run view <RUN_ID --log-failed
gh run rerun <RUN_ID
gh run rerun <RUN_ID --failed
gh workflow run ci.yml --ref main
gh workflow run deploy.yml -f environment=staging
`
``**Với độ cong:**
`bash
# List workflows
curl -s \
-H "Authorization: token $GitHub_TOKEN" \
https://API.GitHub.com/repos/$OWNER/$REPO/actions/workflows \
| Python3 -c "
import sys, JSON
for w in JSON.load(sys.stdin)['workflows']:
print(f\" \{w['id']:10} \{w['name']:30} \{w['state']}\")"
# List recent runs
curl -s \
-H "Authorization: token $GitHub_TOKEN" \
"https://API.GitHub.com/repos/$OWNER/$REPO/actions/runs?per_page=10" \
| Python3 -c "
import sys, JSON
for r in JSON.load(sys.stdin)['workflow_runs']:
print(f\" Run \{r['id']} \{r['name']:30} \{r['conclusion'] or r['status']}\")"
# Download failed run logs
RUN_ID=<run_id
curl -s -L \
-H "Authorization: token $GitHub_TOKEN" \
https://API.GitHub.com/repos/$OWNER/$REPO/actions/runs/$RUN_ID/logs \
-o /tmp/ci-logs.zip
cd /tmp && unzip -o ci-logs.zip -d ci-logs
# Re-run a failed workflow
curl -s -X POST \
-H "Authorization: token $GitHub_TOKEN" \
https://API.GitHub.com/repos/$OWNER/$REPO/actions/runs/$RUN_ID/rerun
# Re-run only failed jobs
curl -s -X POST \
-H "Authorization: token $GitHub_TOKEN" \
https://API.GitHub.com/repos/$OWNER/$REPO/actions/runs/$RUN_ID/rerun-failed-jobs
# Trigger a workflow manually (workflow_dispatch)
WORKFLOW_ID=<workflow_id_or_filename
curl -s -X POST \
-H "Authorization: token $GitHub_TOKEN" \
https://API.GitHub.com/repos/$OWNER/$REPO/actions/workflows/$WORKFLOW_ID/dispatches \
-d '\{"ref": "main", "inputs": \{"environment": "staging"}}'
`
## 10. Ý chính`**Với gh:**
``` bash
gh gist create script.py --public --desc "Useful script"
gh gist list
`
``**Với độ cong:**
`bash
# Create a gist
curl -s -X POST \
-H "Authorization: token $GitHub_TOKEN" \
https://API.GitHub.com/gists \
-d '{
"description": "Useful script",
"public": true,
"files": {
"script.py": \{"content": "print(\"hello\")"}
}
}'
# List your gists
curl -s \
-H "Authorization: token $GitHub_TOKEN" \
https://API.GitHub.com/gists \
| Python3 -c "
import sys, JSON
for g in JSON.load(sys.stdin):
files = ', '.join(g['files'].keys())
print(f\" \{g['id']} \{g['description'] or '(no desc)':40} \{files}\")"
`
## Bảng tham khảo nhanh
| Hành động | gh | git + cuộn tròn |
|--------|------|----------|
| Bản sao |
gh repo clone o/r
` |
`git clone https://GitHub.com/o/r.git
` |
| Tạo kho lưu trữ |
gh repo create name --public
` |
`curl POST /user/repos
` |
| cái nĩa |
gh repo fork o/r --clone
` |
`curl POST /repos/o/r/forks
+
`git clone
` |
| Thông tin repo |
gh repo view o/r
` |
`curl GET /repos/o/r
` |
| Chỉnh sửa cài đặt |
gh repo edit --...
` |
`curl PATCH /repos/o/r
` |
| Tạo bản phát hành |
gh release create v1.0
` |
`curl POST /repos/o/r/releases
` |
| Liệt kê quy trình công việc |
gh workflow list
` |
`curl GET /repos/o/r/actions/workflows
` |
| Chạy lại CI |
gh run rerun ID
` |
`curl POST /repos/o/r/actions/runs/ID/rerun
` |
| Đặt bí mật |
gh secret set KEY
` |
`curl PUT /repos/o/r/actions/secrets/KEY
` (+ mã hóa) |