選單

[課程筆記] Claude Code 入門指南與最佳實踐 (一)

    本文整理自

    管理專案記憶

    • /initCLAUDE.md: 在專案目錄執行 /init,Claude Code 會掃描你的程式碼並生成 CLAUDE.md 摘要。此檔案會在每次會話自動載入,記錄專案結構、重要指令、架構模式與程式風格。(你也可以手動編輯 CLAUDE.md 以新增細節。)

    • #(記憶註解): 使用 # 符號可在 CLAUDE.md 或對話中加入快速備忘。例如,如果 Claude 重複犯某個錯誤,你可以寫 # <note> 來提醒修正。常見用途包括專案專屬指令或資料庫結構。例如:# use uv to run Python files or add dependencies,或記錄資料庫結構:
      # The vector database has two collections: … 並詳細列出 collection 名稱與欄位。這些 # 註解能幫助 Claude 記住重要脈絡。


    內建指令與工具

    • 對話控制:

      • /clear – 清除當前 Claude Code 對話歷史

      • /compact – 縮短並總結對話歷史(壓縮記憶)

      • ESC – 中斷 Claude 的回覆

      • ESC ESC – 將對話回溯到較早的時間點

    • 檔案引用: 使用 @filename 可將檔案內容納入提示,例如 @main.py

    • Shell 與工具: 在任何 shell 指令前加 ! 可在 Claude Code 的終端執行。例如:!pwd!npm test。這允許你互動式執行測試或 Git 指令。輸入 exit 可離開 Claude Code。

    • MCP 與快捷鍵: /mcp 可管理 Model Context Protocol 伺服器。快捷鍵包括 Shift+Tab 在規劃模式與自動接受模式切換,以及 OS 特定的截圖快捷鍵(例如 Windows: Win+Shift+S)。


    擴展思考模式

    • “Think”, “Think hard”, “Ultrathink”: 對於複雜任務(如大型重構、架構規劃、棘手錯誤),可在提示前加上這些指令。不同層級會分配更多內部思考資源,例如:
      think hard: propose a new module structure for this legacy app”
      將觸發更深入的規劃。

    子代理支援(Tasks)

    • 內建子代理: Claude Code 內建 Task 工具,可啟動 AI 子代理進行多步驟或平行工作。例如可使用 /agents 查看與建立子代理。預設子代理包括「code-reviewer」與「debugger」。

    • 使用子代理: 你可直接要求 Claude 使用子代理,例如:「use the code-reviewer subagent to check the auth module」。子代理會在獨立上下文中運行,能進行平行調查(如一個除錯,另一個寫文件)。

    • 自訂子代理: 進階用戶可在 .claude/agents/ 下自訂子代理,設定專屬的系統提示與工具組合(此功能不在入門文件中涵蓋,但受支援)。這可針對專案需求量身打造。


    開發工作流程

    • 理解程式碼: 開始時可請 Claude 總結程式碼與架構。例如:「give me an overview of this codebase」或「explain the main architecture patterns used here」。之後可逐步深入,如:「what are the key data models?」、「how is authentication handled?」。

    • 尋找程式碼: 請 Claude 找出相關檔案或追蹤執行路徑。例如:「find files handling user authentication」或「trace the login process from front-end to database」。

    • 執行測試: Claude 可幫助執行測試並修復錯誤。使用 !npm test!pytest,再將錯誤訊息提供給 Claude,例如:「I see this error when running tests」。常見工作流程包括測試驅動或錯誤修復。

    • 除錯錯誤: 當遇到錯誤時,分享訊息並請 Claude 提供修正方案。例如:「I’m seeing an error when I run npm test」 → 「suggest a few ways to fix the issue」。

    • 程式重構: 請 Claude 分段提供重構建議。例如:「find deprecated API usage in our codebase」,接著「suggest how to refactor utils.js to modern JS」,最後「refactor utils.js to use ES2024 features while maintaining the same behavior」。完成後可重新執行測試確認。

    • 多功能工作流(Git Worktrees): 使用 Git worktrees 可同時處理多個功能分支。例如:

      git worktree add ../proj-featureA -b featureA  
      git worktree add ../proj-featureB -b featureB  
      

      在不同目錄可平行開啟 Claude 工作階段,互不干擾。用 git worktree listgit worktree remove 進行管理。

      Read More:https://github.com/https-deeplearning-ai/sc-claude-code-files/blob/main/reading_notes/L5_notes.md

    • 提交與分支整合: 可自由使用 !git 指令,例如 !git status!git commit。Claude 也能幫忙生成 commit message 或 PR 描述。


    GitHub 整合與 Hooks

    • GitHub Actions 與 @claude: Claude Code 可透過官方 GitHub App 與 Actions 整合。在終端執行 /install-github-app 即可安裝並設定。安裝後,你可以在 issue 或 PR 評論中 @claude,請它分析程式、實作功能或修復錯誤。例如:「@claude fix the TypeError in user dashboard」。

    • CLAUDE.md 維護規範: 在專案中統一維護一份 CLAUDE.md,內容包含程式碼風格、審查標準與專案約定。此檔案會作為基準,GitHub Actions 在自動檢查與審查時會依據這份文件執行。

    • Hooks 與自動化: Claude Code 支援 hooks,能在生命週期事件自動執行 shell 指令。例如自動格式化程式碼、強制 lint、記錄指令,或保護檔案不被編輯。可透過 /hooks 設定,例如 PreToolUse 可記錄 Claude 執行的每個指令。

    • CI/CD 整合: 除 GitHub Actions 外,也可在 pipeline 中整合 Claude。例如在 build 中加入 npm run lint:claude,或用 cat error.txt | claude -p 'explain this error' 分析錯誤日誌。Claude Code 的 --output-format json 亦可輸出結構化結果,方便自動化。