本文件中的每個任務都包含清楚的指示、範例指令和最佳實務,幫助您充分利用 Claude Code。

理解新的程式碼庫

快速獲得程式碼庫概覽

假設您剛加入一個新專案,需要快速了解其結構。

1

導航到專案根目錄

cd /path/to/project 
2

啟動 Claude Code

claude 
3

請求高階概覽

> give me an overview of this codebase 
4

深入了解特定組件

> explain the main architecture patterns used here 
> what are the key data models?
> how is authentication handled?

提示:

  • 從廣泛的問題開始,然後縮小到特定領域
  • 詢問專案中使用的編碼慣例和模式
  • 請求專案特定術語的詞彙表

尋找相關程式碼

假設您需要找到與特定功能或功能性相關的程式碼。

1

請 Claude 尋找相關檔案

> find the files that handle user authentication 
2

獲得組件如何互動的上下文

> how do these authentication files work together? 
3

理解執行流程

> trace the login process from front-end to database 

提示:

  • 對您要尋找的內容要具體
  • 使用專案中的領域語言

高效修復錯誤

假設您遇到了錯誤訊息,需要找到並修復其來源。

1

與 Claude 分享錯誤

> I'm seeing an error when I run npm test 
2

請求修復建議

> suggest a few ways to fix the @ts-ignore in user.ts 
3

應用修復

> update user.ts to add the null check you suggested 

提示:

  • 告訴 Claude 重現問題的指令並獲得堆疊追蹤
  • 提及任何重現錯誤的步驟
  • 讓 Claude 知道錯誤是間歇性的還是持續性的

重構程式碼

假設您需要更新舊程式碼以使用現代模式和實務。

1

識別需要重構的舊程式碼

> find deprecated API usage in our codebase 
2

獲得重構建議

> suggest how to refactor utils.js to use modern JavaScript features 
3

安全地應用變更

> refactor utils.js to use ES2024 features while maintaining the same behavior 
4

驗證重構

> run tests for the refactored code 

提示:

  • 請 Claude 解釋現代方法的好處
  • 在需要時請求變更保持向後相容性
  • 以小的、可測試的增量進行重構

處理測試

假設您需要為未覆蓋的程式碼添加測試。

1

識別未測試的程式碼

> find functions in NotificationsService.swift that are not covered by tests 
2

生成測試架構

> add tests for the notification service 
3

添加有意義的測試案例

> add test cases for edge conditions in the notification service 
4

執行並驗證測試

> run the new tests and fix any failures 

提示:

  • 請求涵蓋邊緣情況和錯誤條件的測試
  • 在適當時請求單元測試和整合測試
  • 讓 Claude 解釋測試策略

建立拉取請求

假設您需要為您的變更建立一個文件完善的拉取請求。

1

總結您的變更

> summarize the changes I've made to the authentication module 
2

使用 Claude 生成 PR

> create a pr 
3

檢視並完善

> enhance the PR description with more context about the security improvements 
4

添加測試詳情

> add information about how these changes were tested 

提示:

  • 直接請 Claude 為您建立 PR
  • 在提交前檢視 Claude 生成的 PR
  • 請 Claude 強調潛在風險或考慮事項

處理文件

假設您需要為您的程式碼添加或更新文件。

1

識別未文件化的程式碼

> find functions without proper JSDoc comments in the auth module 
2

生成文件

> add JSDoc comments to the undocumented functions in auth.js 
3

檢視並增強

> improve the generated documentation with more context and examples 
4

驗證文件

> check if the documentation follows our project standards 

提示:

  • 指定您想要的文件風格(JSDoc、docstrings 等)
  • 在文件中請求範例
  • 請求公共 API、介面和複雜邏輯的文件

處理圖片

假設您需要在程式碼庫中處理圖片,並希望 Claude 幫助分析圖片內容。

1

將圖片添加到對話中

您可以使用以下任何方法:

  1. 將圖片拖放到 Claude Code 視窗中
  2. 複製圖片並使用 ctrl+v 貼到 CLI 中(不要使用 cmd+v)
  3. 提供圖片路徑給 claude。例如,“Analyze this image: /path/to/your/image.png”
2

請 Claude 分析圖片

> What does this image show?
> Describe the UI elements in this screenshot
> Are there any problematic elements in this diagram?
3

使用圖片作為上下文

> Here's a screenshot of the error. What's causing it?
> This is our current database schema. How should we modify it for the new feature?
4

從視覺內容獲得程式碼建議

> Generate CSS to match this design mockup
> What HTML structure would recreate this component?

提示:

  • 當文字描述不清楚或繁瑣時使用圖片
  • 包含錯誤截圖、UI 設計或圖表以獲得更好的上下文
  • 您可以在對話中處理多張圖片
  • 圖片分析適用於圖表、截圖、模型圖等

使用延伸思考

假設您正在處理複雜的架構決策、具挑戰性的錯誤,或規劃需要深度推理的多步驟實作。

1

提供上下文並請 Claude 思考

> I need to implement a new authentication system using OAuth2 for our API. Think deeply about the best approach for implementing this in our codebase. 

Claude 將從您的程式碼庫收集相關資訊並 使用延伸思考,這將在介面中可見。

2

使用後續提示完善思考

> think about potential security vulnerabilities in this approach 
> think harder about edge cases we should handle 

從延伸思考獲得最大價值的提示:

延伸思考對複雜任務最有價值,例如:

  • 規劃複雜的架構變更
  • 除錯複雜問題
  • 為新功能建立實作計劃
  • 理解複雜的程式碼庫
  • 評估不同方法之間的權衡

您提示思考的方式會產生不同深度的思考:

  • “think” 觸發基本延伸思考
  • 強化短語如 “think more”、“think a lot”、“think harder” 或 “think longer” 觸發更深層的思考

更多延伸思考提示技巧,請參閱延伸思考提示

Claude 將在回應上方以斜體灰色文字顯示其思考過程。


恢復先前的對話

假設您一直在使用 Claude Code 處理任務,需要在稍後的會話中繼續之前的工作。

Claude Code 提供兩個恢復先前對話的選項:

  • --continue 自動繼續最近的對話
  • --resume 顯示對話選擇器
1

繼續最近的對話

claude --continue

這會立即恢復您最近的對話,無需任何提示。

2

在非互動模式中繼續

claude --continue --print "Continue with my task"

使用 --print--continue 在非互動模式中恢復最近的對話,非常適合腳本或自動化。

3

顯示對話選擇器

claude --resume

這會顯示一個互動式對話選擇器,顯示:

  • 對話開始時間
  • 初始提示或對話摘要
  • 訊息數量

使用方向鍵導航並按 Enter 選擇對話。

提示:

  • 對話歷史儲存在您的機器本地
  • 使用 --continue 快速存取您最近的對話
  • 當您需要選擇特定過去對話時使用 --resume
  • 恢復時,您會在繼續前看到整個對話歷史
  • 恢復的對話以與原始對話相同的模型和配置開始

運作方式:

  1. 對話儲存:所有對話都會自動與其完整訊息歷史一起儲存在本地
  2. 訊息反序列化:恢復時,整個訊息歷史會被還原以維持上下文
  3. 工具狀態:先前對話中的工具使用和結果會被保留
  4. 上下文還原:對話會在所有先前上下文完整的情況下恢復

範例:

# 繼續最近的對話
claude --continue

# 使用特定提示繼續最近的對話
claude --continue --print "Show me our progress"

# 顯示對話選擇器
claude --resume

# 在非互動模式中繼續最近的對話
claude --continue --print "Run the tests again"

使用 Git worktrees 執行並行 Claude Code 會話

假設您需要同時處理多個任務,並在 Claude Code 實例之間完全隔離程式碼。

1

理解 Git worktrees

Git worktrees 允許您從同一個儲存庫將多個分支檢出到不同的目錄中。每個 worktree 都有自己的工作目錄和隔離的檔案,同時共享相同的 Git 歷史。在官方 Git worktree 文件中了解更多。

2

建立新的 worktree

# 使用新分支建立新的 worktree
git worktree add ../project-feature-a -b feature-a

# 或使用現有分支建立 worktree
git worktree add ../project-bugfix bugfix-123

這會建立一個新目錄,其中包含您儲存庫的獨立工作副本。

3

在每個 worktree 中執行 Claude Code

# 導航到您的 worktree
cd ../project-feature-a

# 在這個隔離環境中執行 Claude Code
claude
4

在另一個 worktree 中執行 Claude

cd ../project-bugfix
claude
5

管理您的 worktrees

# 列出所有 worktrees
git worktree list

# 完成後移除 worktree
git worktree remove ../project-feature-a

提示:

  • 每個 worktree 都有自己獨立的檔案狀態,非常適合並行 Claude Code 會話
  • 在一個 worktree 中所做的變更不會影響其他 worktree,防止 Claude 實例相互干擾
  • 所有 worktrees 共享相同的 Git 歷史和遠端連接
  • 對於長時間執行的任務,您可以讓 Claude 在一個 worktree 中工作,同時在另一個中繼續開發
  • 使用描述性目錄名稱輕鬆識別每個 worktree 用於哪個任務
  • 記住根據您專案的設定在每個新 worktree 中初始化您的開發環境。根據您的技術堆疊,這可能包括:
    • JavaScript 專案:執行依賴安裝(npm installyarn
    • Python 專案:設定虛擬環境或使用套件管理器安裝
    • 其他語言:遵循您專案的標準設定流程

將 Claude 用作 unix 風格的工具

將 Claude 添加到您的驗證流程

假設您想將 Claude Code 用作 linter 或程式碼審查器。

將 Claude 添加到您的建置腳本:

// package.json
{
    ...
    "scripts": {
        ...
        "lint:claude": "claude -p 'you are a linter. please look at the changes vs. main and report any issues related to typos. report the filename and line number on one line, and a description of the issue on the second line. do not return any other text.'"
    }
}

提示:

  • 在您的 CI/CD 管道中使用 Claude 進行自動化程式碼審查
  • 自訂提示以檢查與您專案相關的特定問題
  • 考慮為不同類型的驗證建立多個腳本

管道輸入,管道輸出

假設您想將資料管道輸入 Claude,並以結構化格式取回資料。

透過 Claude 管道資料:

cat build-error.txt | claude -p 'concisely explain the root cause of this build error' > output.txt

提示:

  • 使用管道將 Claude 整合到現有的 shell 腳本中
  • 與其他 Unix 工具結合以實現強大的工作流程
  • 考慮使用 —output-format 進行結構化輸出

控制輸出格式

假設您需要 Claude 的輸出採用特定格式,特別是在將 Claude Code 整合到腳本或其他工具中時。

1

使用文字格式(預設)

cat data.txt | claude -p 'summarize this data' --output-format text > summary.txt

這只輸出 Claude 的純文字回應(預設行為)。

2

使用 JSON 格式

cat code.py | claude -p 'analyze this code for bugs' --output-format json > analysis.json

這輸出包含成本和持續時間等元資料的 JSON 訊息陣列。

3

使用串流 JSON 格式

cat log.txt | claude -p 'parse this log file for errors' --output-format stream-json

這會在 Claude 處理請求時即時輸出一系列 JSON 物件。每個訊息都是有效的 JSON 物件,但如果連接起來,整個輸出不是有效的 JSON。

提示:

  • 對於只需要 Claude 回應的簡單整合,使用 --output-format text
  • 當您需要完整對話日誌時使用 --output-format json
  • 對於每個對話回合的即時輸出,使用 --output-format stream-json

建立自訂斜線指令

Claude Code 支援自訂斜線指令,您可以建立這些指令來快速執行特定提示或任務。

更多詳情,請參閱斜線指令參考頁面。

建立專案特定指令

假設您想為您的專案建立可重複使用的斜線指令,所有團隊成員都可以使用。

1

在您的專案中建立指令目錄

mkdir -p .claude/commands
2

為每個指令建立 Markdown 檔案

echo "Analyze the performance of this code and suggest three specific optimizations:" > .claude/commands/optimize.md 
3

在 Claude Code 中使用您的自訂指令

> /project:optimize 

提示:

  • 指令名稱來自檔案名稱(例如,optimize.md 變成 /project:optimize
  • 您可以在子目錄中組織指令(例如,.claude/commands/frontend/component.md 變成 /project:frontend:component
  • 專案指令對所有複製儲存庫的人都可用
  • Markdown 檔案內容在調用指令時成為發送給 Claude 的提示

使用 $ARGUMENTS 添加指令參數

假設您想建立可以接受使用者額外輸入的靈活斜線指令。

1

使用 $ARGUMENTS 佔位符建立指令檔案

echo "Find and fix issue #$ARGUMENTS. Follow these steps: 1.
Understand the issue described in the ticket 2. Locate the relevant code in
our codebase 3. Implement a solution that addresses the root cause 4. Add
appropriate tests 5. Prepare a concise PR description" >
.claude/commands/fix-issue.md 
2

使用帶有問題編號的指令

在您的 Claude 會話中,使用帶有參數的指令。

> /project:fix-issue 123 

這會在提示中將 $ARGUMENTS 替換為 “123”。

提示:

  • $ARGUMENTS 佔位符會被指令後面的任何文字替換
  • 您可以在指令範本中的任何位置放置 $ARGUMENTS
  • 其他有用的應用:為特定函數生成測試案例、為組件建立文件、審查特定檔案中的程式碼,或將內容翻譯為指定語言

建立個人斜線指令

假設您想建立在所有專案中都能使用的個人斜線指令。

1

在您的主資料夾中建立指令目錄

mkdir -p ~/.claude/commands 
2

為每個指令建立 Markdown 檔案

echo "Review this code for security vulnerabilities, focusing on:" >
~/.claude/commands/security-review.md 
3

使用您的個人自訂指令

> /user:security-review 

提示:

  • 個人指令以 /user: 而不是 /project: 為前綴
  • 個人指令只對您可用,不與您的團隊共享
  • 個人指令在您所有的專案中都能使用
  • 您可以將這些用於不同程式碼庫之間的一致工作流程

下一步

Claude Code 參考實作

複製我們的開發容器參考實作。