GitHub 入門
GitHub 是全球最大的程式碼托管平台,除了存放程式碼,還提供協作工具、CI/CD、專案管理等功能。
什麼是 GitHub?
- 程式碼托管:在雲端儲存你的 Git repository
- 協作平台:Pull Request、Code Review、Issue 追蹤
- 開源社群:探索、使用、貢獻開源專案
- 開發者社群:個人作品集、履歷展示
GitHub 是免費的,私人 repository 也免費(有部分功能限制)。
建立 GitHub 帳號
- 前往 github.com
- 點擊 Sign up
- 輸入 Email、密碼、使用者名稱
- 完成驗證
建立 Repository
在 GitHub 網站建立
- 點擊右上角的 + → New repository
- 填寫 Repository name
- 選擇 Public 或 Private
- 可選:勾選 Initialize with README
- 點擊 Create repository
連結本地專案到 GitHub
如果你已經有本地的 Git repository:
# 1. 在 GitHub 建立空的 repository(不要勾選 Initialize)
# 2. 在本地專案執行
git remote add origin https://github.com/username/repo.git
git branch -M main
git push -u origin main
從 GitHub Clone 專案
# HTTPS
git clone https://github.com/username/repo.git
# SSH
git clone git@github.com:username/repo.git
Pull Request (PR)
Pull Request 是 GitHub 最重要的協作功能,讓你提議把一個分支的變更合併到另一個分支。
建立 Pull Request
- 推送你的分支到 GitHub
git checkout -b feature/new-feature
# 做一些變更...
git push -u origin feature/new-feature
- 在 GitHub 點擊 Compare & pull request
- 填寫 PR 標題和描述
- 點擊 Create pull request
PR 流程
1. 建立功能分支
2. 開發並 commit
3. Push 到 GitHub
4. 開 Pull Request
5. Code Review
6. 討論、修改
7. Approve
8. Merge
9. 刪除分支
Merge 選項
GitHub 提供三種 merge 方式:
- Create a merge commit:保留所有 commit,產生 merge commit
- Squash and merge:把所有 commit 合併成一個
- Rebase and merge:rebase 後 fast-forward merge
Issue
Issue 用來追蹤 bug、功能需求、討論等。
建立 Issue
- 進入 repository 的 Issues 頁籤
- 點擊 New issue
- 填寫標題和內容
- 可加上 Labels、Assignees、Projects 等
連結 Issue 和 PR
在 commit 訊息或 PR 描述中使用關鍵字:
git commit -m "Fix login bug, closes #123"
當 PR 被 merge,#123 issue 會自動被關閉。
關鍵字:close、closes、closed、fix、fixes、fixed、resolve、resolves、resolved
Fork
Fork 讓你複製別人的 repository 到你的帳號下,常用於貢獻開源專案。
Fork 流程
- 在別人的 repository 點擊 Fork
- Clone 你的 fork
git clone https://github.com/yourname/repo.git
- 新增 upstream(原始 repository)
git remote add upstream https://github.com/original/repo.git
- 建立分支並開發
git checkout -b fix/typo
# 修改...
git push origin fix/typo
- 在 GitHub 開 Pull Request(從你的 fork 到原始 repo)
同步 Upstream
git fetch upstream
git checkout main
git merge upstream/main
git push origin main
GitHub Pages
GitHub Pages 讓你免費託管靜態網站。
啟用 GitHub Pages
- 進入 repository 的 Settings
- 找到 Pages 區段
- 選擇來源分支(通常是 main 或 gh-pages)
- 網站會在
https://username.github.io/repo/
個人網站
建立名為 username.github.io 的 repository,網站會在 https://username.github.io/
GitHub Actions
GitHub Actions 是內建的 CI/CD 工具,可以自動化測試、部署等流程。
簡單範例
建立 .github/workflows/test.yml:
name: Test
on:
push:
branches: [main]
pull_request:
branches: [main]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: '20'
- run: npm install
- run: npm test
每次 push 或 PR 到 main 分支時,會自動執行測試。
常用設定
Branch Protection
保護重要分支:
- Settings → Branches → Add rule
- 設定規則:
- Require a pull request before merging
- Require status checks to pass
- Require conversation resolution
.gitignore 模板
建立 repository 時可以選擇 .gitignore 模板。
License
開源專案記得加上 License:
- 建立 repository 時選擇
- 或新增 LICENSE 檔案
常見的 License:MIT、Apache 2.0、GPL
GitHub CLI
GitHub 官方的命令列工具:
# 安裝(macOS)
brew install gh
# 登入
gh auth login
# 建立 repository
gh repo create my-project --public
# 建立 PR
gh pr create
# 查看 PR
gh pr list
# Clone repository
gh repo clone username/repo
實用功能
Keyboard Shortcuts
在 GitHub 頁面按 ? 查看快捷鍵:
t:搜尋檔案w:切換分支l:跳到某一行.:在網頁版 VS Code 開啟
網頁版編輯器
在任何 repository 按 . 或把網址的 github.com 改成 github.dev,會開啟網頁版 VS Code。
搜尋程式碼
在 GitHub 搜尋框使用進階語法:
repo:facebook/react useState
language:javascript fetch api
filename:package.json react
學習資源
- GitHub Skills:互動式教學
- GitHub Docs:官方文件
- Explore GitHub:探索熱門專案