建立、clone、add/commit/push、README、.gitignore
Repo(Repository,倉庫)=「一個專案」的容器。它包含程式碼、版本歷史、Issue、PR、Wiki 等一切。每個 repo 都有一個唯一網址,可以是公開(public)或私有(private)。
.git/)+ GitHub 的服務。hello-world/ ├── .git/ # 版本歷史(隱藏,別亂刪) ├── README.md # 專案門面 ├── src/ # 你的程式碼 └── .gitignore # 排除清單
https://github.com/<你>/<repo名> # clone 用: # https://github.com/你/repo名.git # git@github.com:你/repo名.git (SSH)
1. 右上角「+」→ New repository 2. 填名稱(如 hello-world) 3. 選 Public / Private 4. 可勾選 Add a README(建議新手勾) 5. 按 Create repository
你現在看的教學站,其 repo 就是用這個流程建立的。
裝了 gh CLI 之後,一條指令就能建好 repo 並推上 GitHub(單元 8 會完整介紹 gh):
gh repo create hello-world --public --source=. --push # --source=. 以目前資料夾為內容 # --push 建好後立刻推送
要開始開發,把 repo 抓到你自己的電腦:
git clone https://github.com/you/hello-world.git cd hello-world
hello-world/
├── .git/ # 完整版本歷史
└── README.md.git/ 版本歷史一起抓下來,之後能 pull / push;下載 ZIP 只有「目前的檔案」,無法同步。Git 的日常只有三步,記住就夠了:
git add . # 1. 暫存(stage):挑選要納入的變更 git commit -m "說明" # 2. 提交(commit):存成一個版本快照 git push # 3. 推送(push):上傳到 GitHub
| 指令 | 作用 | 類比 |
|---|---|---|
git add | 把變更加入暫存區(staging area) | 打包要寄的東西 |
git commit | 存成一個帶說明的版本 | 寫郵戳寄出 |
git push | 把本地版本同步到 GitHub | 快遞送到雲端 |
fix: 修正登入頁空白;壞例子:update、改。若你是在本機「先有專案、後想上 GitHub」(沒有先 clone),流程是:
git init -b main # 初始化,主分支叫 main git add . git commit -m "first commit" git remote add origin https://github.com/you/hello-world.git git push -u origin main # -u 記住 upstream,之後直接 git push
remote=「遠端位置」的暱稱,慣例叫 origin;-u(--set-upstream)讓本機記住要推去哪裡。
README 是訪客第一眼看到的文件,用 Markdown 寫(回顧我們的 Markdown 教學站)。夠用的 README 只要四段:
# hello-world A demo project. ## Usage ```bash npm start ``` ## License MIT
# 專案名:一級標題。
一句話簡介:做什麼、解決什麼。
Usage:怎麼安裝、怎麼跑。
License:授權(單元 8 詳談)。
四段就是最小可用 README。
告訴 Git「哪些檔案不要追蹤」——例如 node_modules、建置產物、本機設定與機密:
node_modules/ dist/ .env *.log .DS_Store
.env、金鑰、Token 務必加入 .gitignore。一旦 push 上去,它就進入 Git 歷史——就算之後刪掉,「歷史裡還留著」。真的洩漏了,重點不是刪檔,而是撤銷那個憑證。mkdir my-first-repo && cd my-first-repo git init -b main echo "# my-first-repo" > README.md git add . git commit -m "feat: init repo with README" gh repo create my-first-repo --public --source=. --push
✅ git status 顯示乾淨(nothing to commit)
✅ GitHub 上看到 README.md
✅ 再改一筆 → add / commit / push,GitHub 上歷史多一筆
✅ git log --oneline 看得到 commit
.gitignore,把 .env、*.log、node_modules/ 排除;再推一筆更新,確認 GitHub 上看不到這些檔案。你要建立一個「全端專案」 repo: - frontend/(React,大量 node_modules) - backend/(Python,有 .env 和模型檔案) - docs/(Markdown 教學文件) - assets/(含 200MB 影片檔案) - 需要用 Git LFS 追蹤大檔
# 1. 初始化並建立目錄結構 mkdir fullstack-project && cd fullstack-project git init -b main # 2. 安裝 Git LFS git lfs install # 3. 設定 LFS 追蹤大檔 git lfs track "*.mp4" git lfs track "*.zip" git lfs track "*.psd" echo ".gitattributes" >> .gitignore # 4. 建立 .gitignore(進階) cat > .gitignore << 'EOF' # Node node_modules/ dist/ .next/ # Python __pycache__/ *.pyc .env venv/ # 大檔(由 LFS 管理) *.mp4 *.zip # OS .DS_Store Thumbs.db # IDE .vscode/ .idea/ EOF # 5. 建立 .env.example(提醒團隊) echo "DATABASE_URL=postgres://..." > .env.example # 6. 首次提交 git add . git commit -m "chore: init monorepo with LFS and gitignore" # 7. 建立遠端 repo 並推送 gh repo create TechCo/fullstack-project \ --private --source=. --push # 8. 驗證 LFS 生效 git lfs ls-files
git add → 暫存區(Staging Area / Index) └── 寫入 .git/index(二進位檔案,追蹤哪些 blob 要提交) git commit → 提交對象 └── 寫入 .git/objects/(blob + tree + commit 物件) └── 更新 .git/refs/heads/main(分支指標移到新 commit) git push → 網路傳輸 └── 用 packfile 格式壓縮差異物件 └── 透過 HTTPS (Git Smart HTTP) 或 SSH 傳到 GitHub └── GitHub 端寫入 bare repo + 觸發 Webhook
git gc(garbage collection)會清除沒有被任何分支或 tag 指向的物件。未 push 的 commit 仍會被保留(除非你 git gc --prune=now)。git rm --cached 取消追蹤。| 症狀 | 可能原因 | 解決方案 |
|---|---|---|
fatal: destination path already exists | 目錄已存在 | 先 rm -rf repo名 或改名;或改用 git clone(不要自己 mkdir) |
| push 後 README 沒更新 | 用了 git push --force 覆蓋遠端 | 確認 git log --oneline;用 git revert 回滾(非 force push) |
| .gitignore 的檔案仍出現在 repo | 該檔案已被追蹤 | git rm --cached 檔名 → git commit → git push |
error: failed to push some refs | 遠端有你本機沒有的 commit | git pull --rebase origin main 先同步再推 |
| Git LFS 檔案顯示為文字指標 | 未執行 git lfs pull | git lfs install && git lfs pull |
git lfs ls-files 顯示正確大小。git push --force 與 git push --force-with-lease 的差異,實際用兩個分支模擬場景:為什麼 force-with-lease 更安全?單元 2.9 只推了「一個 README」;這裡建立一個真的要開工的全端 monorepo——frontend、backend、docs、assets 一次到位,含 .gitignore、Git LFS、commit 規範。做完它,你的 repo 就是「可以直接交給團隊」的狀態——這是單元 2.10「單一 repo 進階技巧」之上的整站施工標準。
techco-app/ ├── frontend/ # React + Vite │ ├── src/ │ └── package.json ├── backend/ # Python FastAPI │ ├── app/ │ ├── requirements.txt │ └── .env.example # 範例,絕不提交 .env ├── docs/ │ └── architecture.md ├── assets/ # 大檔 → Git LFS │ └── demo.mp4 ├── .gitattributes # LFS 規則(由 LFS 追蹤) ├── .gitignore ├── .editorconfig └── README.md
# .gitattributes(LFS 規則) *.mp4 filter=lfs diff=lfs merge=lfs -text *.zip filter=lfs diff=lfs merge=lfs -text *.psd filter=lfs diff=lfs merge=lfs -text # .gitignore 重要段落 node_modules/ dist/ __pycache__/ .env *.log # README.md(門面) # TechCo App 全端範例:React + FastAPI ├── 快速開始 ├── 結構說明 └── 開發規範(commit 格式、分支名)
# 1. 確認 .env 與 node_modules 不會被追蹤 git status --porcelain | grep -E '\.env|node_modules' # 輸出 →(空,代表沒被追蹤) # 2. 確認 LFS 生效 git lfs ls-files # 輸出 → 1f8a2b... assets/demo.mp4 (LFS: 24.5 MB) # 3. 檢查 repo 實體體積(不含 LFS 大檔) git count-objects -vH | grep size-pack # 輸出 → size-pack: 說明 repo 保持輕量 # 4. 全新機器 clone 驗證(LFS 自動下載) git clone git@github.com:TechCo/techco-app.git git lfs pull
repo 尺寸管理: - 大檔走 LFS,避免 repo 肥大 - GitHub 警示:repo 超過 1GB 應處理 clone 加速: - --depth=1 淺克隆(CI 常用) - --filter=blob:none 只抓歷史結構 - sparse-checkout 只 checkout 需要的目錄 傳輸: - 每次 push 只送 packfile 差異,而非整個檔案
repo 就是產品門面: - README:一句話說明 + 快速開始 - .editorconfig:統一縮排與換行 - commit 訊息規範(feat:/fix:/docs:) - 開啟 branch protection 或 repo rulesets 內務:.gitignore 寫完整 → 別人不小心 提交 .env 時,檢核與 CI 會擋下。
第一道防線在 repo 邊界: - .env / 憑證檔 100% 進 .gitignore - 提交前用 pre-commit hook 掃 secret - 開啟 Secret scanning 與 push protection - 已洩漏:先撤銷憑證 > 再清理歷史(filter-repo) - 最小權限:只有需要的人能寫入
| 面向 | github | gitlab | markdown | yaml | json |
|---|---|---|---|---|---|
| repo 建立 | gh repo create / 網頁按鈕 | GitLab 專案創建 / API | —(不涉及) | —(不涉及) | —(不涉及) |
| 託管與私有 | GitHub.com / Enterprise Server | SaaS / 自架 GitLab CE/EE | — | — | — |
| 大型檔案 | Git LFS(單檔上限 2GB) | Git LFS(額度策略不同) | — | — | — |
| 檔案內容形式 | 以 Markdown 寫 README / 文件 | 同左 | README 與文件的主要語法 | CI 設定(.gitlab-ci.yml) | GitHub / GitLab API 的傳輸格式 |
| 協作入口 | Issue / PR 按鈕 | Issue / MR 按鈕 | — | — | — |