本文件中的每個任務都包含清楚的指示、範例命令和最佳實踐,幫助您充分利用 Claude Code。
理解新的程式碼庫
快速獲得程式碼庫概覽
假設您剛加入一個新專案,需要快速理解其結構。
要求高層次概覽
> 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 the files that handle user authentication
獲得組件如何互動的上下文
> how do these authentication files work together?
理解執行流程
> trace the login process from front-end to database
高效修復錯誤
假設您遇到了錯誤訊息,需要找到並修復其來源。
與 Claude 分享錯誤
> I'm seeing an error when I run npm test
要求修復建議
> suggest a few ways to fix the @ts-ignore in user.ts
應用修復
> update user.ts to add the null check you suggested
提示:
告訴 Claude 重現問題的命令並獲得堆疊追蹤
提及任何重現錯誤的步驟
讓 Claude 知道錯誤是間歇性的還是持續性的
重構程式碼
假設您需要更新舊程式碼以使用現代模式和實踐。
識別需要重構的遺留程式碼
> find deprecated API usage in our codebase
獲得重構建議
> suggest how to refactor utils.js to use modern JavaScript features
安全地應用變更
> refactor utils.js to use ES2024 features while maintaining the same behavior
驗證重構
> run tests for the refactored code
提示:
要求 Claude 解釋現代方法的好處
在需要時要求變更保持向後相容性
以小的、可測試的增量進行重構
使用專門的子代理
假設您想使用專門的 AI 子代理來更有效地處理特定任務。
自動使用子代理
Claude Code 會自動將適當的任務委派給專門的子代理: > review my recent code changes for security issues
> run all tests and fix any failures
明確要求特定子代理
> use the code-reviewer subagent to check the auth module
> have the debugger subagent investigate why users can't log in
為您的工作流程建立自訂子代理
然後選擇「建立新子代理」並按照提示定義:
子代理類型(例如,api-designer、performance-optimizer)
何時使用它
它可以存取哪些工具
其專門的系統提示
提示:
在 .claude/agents/ 中建立專案特定的子代理以供團隊共享
使用描述性的 description 欄位來啟用自動委派
將工具存取限制為每個子代理實際需要的內容
查看子代理文件 以獲得詳細範例
使用計劃模式進行安全的程式碼分析
計劃模式指示 Claude 透過唯讀操作分析程式碼庫來建立計劃,非常適合探索程式碼庫、規劃複雜變更或安全地審查程式碼。
何時使用計劃模式
多步驟實作 :當您的功能需要對許多檔案進行編輯時
程式碼探索 :當您想在變更任何內容之前徹底研究程式碼庫時
互動式開發 :當您想與 Claude 就方向進行迭代時
如何使用計劃模式
在會話期間開啟計劃模式
您可以在會話期間使用 Shift+Tab 循環切換權限模式來切換到計劃模式。
如果您處於正常模式,Shift+Tab 會首先切換到自動接受模式,在終端底部顯示 ⏵⏵ accept edits on。再次按 Shift+Tab 會切換到計劃模式,顯示 ⏸ plan mode on。
以計劃模式開始新會話
要以計劃模式開始新會話,請使用 --permission-mode plan 標誌:
claude --permission-mode plan
在計劃模式下執行「無頭」查詢
您也可以使用 -p 直接在計劃模式下執行查詢(即在「無頭模式」 中):
claude --permission-mode plan -p "Analyze the authentication system and suggest improvements"
範例:規劃複雜重構
claude --permission-mode plan
> I need to refactor our authentication system to use OAuth2. Create a detailed migration plan.
Claude 會分析目前的實作並建立全面的計劃。使用後續問題進行細化:
> What about backward compatibility?
> How should we handle database migration?
將計劃模式設定為預設
// .claude/settings.json
{
"permissions" : {
"defaultMode" : "plan"
}
}
查看設定文件 以獲得更多配置選項。
處理測試
假設您需要為未覆蓋的程式碼新增測試。
識別未測試的程式碼
> find functions in NotificationsService.swift that are not covered by tests
產生測試腳手架
> add tests for the notification service
新增有意義的測試案例
> add test cases for edge conditions in the notification service
執行並驗證測試
> run the new tests and fix any failures
提示:
要求涵蓋邊緣案例和錯誤條件的測試
在適當時要求單元測試和整合測試
讓 Claude 解釋測試策略
建立拉取請求
假設您需要為您的變更建立一個記錄良好的拉取請求。
總結您的變更
> summarize the changes I've made to the authentication module
審查並細化
> enhance the PR description with more context about the security improvements
新增測試詳細資訊
> add information about how these changes were tested
提示:
直接要求 Claude 為您建立 PR
在提交前審查 Claude 產生的 PR
要求 Claude 突出潛在風險或考慮事項
處理文件
假設您需要為您的程式碼新增或更新文件。
識別未記錄的程式碼
> find functions without proper JSDoc comments in the auth module
產生文件
> add JSDoc comments to the undocumented functions in auth.js
審查並增強
> improve the generated documentation with more context and examples
驗證文件
> check if the documentation follows our project standards
提示:
指定您想要的文件樣式(JSDoc、docstrings 等)
在文件中要求範例
要求為公共 API、介面和複雜邏輯提供文件
處理圖片
假設您需要在程式碼庫中處理圖片,並希望 Claude 幫助分析圖片內容。
將圖片新增到對話中
您可以使用以下任何方法:
將圖片拖放到 Claude Code 視窗中
複製圖片並使用 ctrl+v 貼到 CLI 中(不要使用 cmd+v)
向 Claude 提供圖片路徑。例如,「分析這張圖片:/path/to/your/image.png」
要求 Claude 分析圖片
> What does this image show?
> Describe the UI elements in this screenshot
> Are there any problematic elements in this diagram?
使用圖片作為上下文
> 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?
從視覺內容獲得程式碼建議
> Generate CSS to match this design mockup
> What HTML structure would recreate this component?
提示:
當文字描述不清楚或繁瑣時使用圖片
包含錯誤截圖、UI 設計或圖表以獲得更好的上下文
您可以在對話中處理多張圖片
圖片分析適用於圖表、截圖、模型等
引用檔案和目錄
使用 @ 快速包含檔案或目錄,無需等待 Claude 讀取它們。
引用單個檔案
> Explain the logic in @src/utils/auth.js
這會將檔案的完整內容包含在對話中。
引用目錄
> What's the structure of @src/components?
這會提供包含檔案資訊的目錄清單。
引用 MCP 資源
> Show me the data from @github:repos/owner/repo/issues
這會使用格式 @server:resource 從連接的 MCP 伺服器獲取資料。查看 MCP 資源 以獲得詳細資訊。
提示:
檔案路徑可以是相對的或絕對的
@ 檔案引用會將檔案目錄和父目錄中的 CLAUDE.md 新增到上下文中
目錄引用顯示檔案清單,而不是內容
您可以在單個訊息中引用多個檔案(例如,「@file1.js and @file2.js」)
使用擴展思考
假設您正在處理複雜的架構決策、具有挑戰性的錯誤或需要深度推理的多步驟實作規劃。
提供上下文並要求 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 會從您的程式碼庫收集相關資訊並
使用擴展思考,這將在介面中可見。
使用後續提示細化思考
> think about potential security vulnerabilities in this approach
> think hard about edge cases we should handle
從擴展思考中獲得最大價值的提示: 擴展思考 對於複雜任務最有價值,例如:
規劃複雜的架構變更
除錯複雜問題
為新功能建立實作計劃
理解複雜的程式碼庫
評估不同方法之間的權衡
在會話期間使用 Tab 切換思考開啟和關閉。 您提示思考的方式會導致不同程度的思考深度:
「think」觸發基本擴展思考
強化短語如「keep hard」、「think more」、「think a lot」或「think longer」觸發更深層的思考
有關更多擴展思考提示技巧,請參閱擴展思考技巧 。
Claude 會在回應上方以斜體灰色文字顯示其思考過程。
恢復先前的對話
假設您一直在使用 Claude Code 處理任務,需要在稍後的會話中繼續之前的工作。
Claude Code 提供兩個恢復先前對話的選項:
--continue 自動繼續最近的對話
--resume 顯示對話選擇器
在非互動模式下繼續
claude --continue --print "Continue with my task"
使用 --print 與 --continue 在非互動模式下恢復最近的對話,非常適合腳本或自動化。
顯示對話選擇器
這會顯示一個互動式對話選擇器,具有清潔的清單檢視,顯示:
會話摘要(或初始提示)
元資料:經過時間、訊息數量和 git 分支
使用方向鍵導航並按 Enter 選擇對話。按 Esc 退出。
提示:
對話歷史記錄儲存在您的機器本地
使用 --continue 快速存取您最近的對話
當您需要選擇特定的過去對話時使用 --resume
恢復時,您會在繼續之前看到整個對話歷史記錄
恢復的對話以與原始對話相同的模型和配置開始
工作原理:
對話儲存 :所有對話都會自動儲存在本地,包含完整的訊息歷史記錄
訊息反序列化 :恢復時,整個訊息歷史記錄會被還原以維持上下文
工具狀態 :先前對話中的工具使用和結果會被保留
上下文還原 :對話會在保持所有先前上下文完整的情況下恢復
範例: # 繼續最近的對話
claude --continue
# 使用特定提示繼續最近的對話
claude --continue --print "Show me our progress"
# 顯示對話選擇器
claude --resume
# 在非互動模式下繼續最近的對話
claude --continue --print "Run the tests again"
使用 Git worktrees 執行並行 Claude Code 會話
假設您需要同時處理多個任務,並在 Claude Code 實例之間完全隔離程式碼。
理解 Git worktrees
Git worktrees 允許您從同一個儲存庫將多個分支檢出到不同的目錄中。每個 worktree 都有自己的工作目錄和隔離的檔案,同時共享相同的 Git 歷史記錄。在官方 Git worktree 文件 中了解更多。
建立新的 worktree
# 使用新分支建立新的 worktree
git worktree add ../project-feature-a -b feature-a
# 或使用現有分支建立 worktree
git worktree add ../project-bugfix bugfix-123
這會建立一個新目錄,其中包含您儲存庫的單獨工作副本。
在每個 worktree 中執行 Claude Code
# 導航到您的 worktree
cd ../project-feature-a
# 在這個隔離環境中執行 Claude Code
claude
在另一個 worktree 中執行 Claude
cd ../project-bugfix
claude
管理您的 worktrees
# 列出所有 worktrees
git worktree list
# 完成後移除 worktree
git worktree remove ../project-feature-a
提示:
每個 worktree 都有自己獨立的檔案狀態,非常適合並行 Claude Code 會話
在一個 worktree 中所做的變更不會影響其他 worktree,防止 Claude 實例相互干擾
所有 worktrees 共享相同的 Git 歷史記錄和遠端連接
對於長時間執行的任務,您可以讓 Claude 在一個 worktree 中工作,同時在另一個 worktree 中繼續開發
使用描述性目錄名稱來輕鬆識別每個 worktree 用於哪個任務
記住根據您專案的設定在每個新 worktree 中初始化您的開發環境。根據您的技術堆疊,這可能包括:
JavaScript 專案:執行相依性安裝(npm install、yarn)
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 整合到腳本或其他工具中時。
使用文字格式(預設)
cat data.txt | claude -p 'summarize this data' --output-format text > summary.txt
這只輸出 Claude 的純文字回應(預設行為)。
使用 JSON 格式
cat code.py | claude -p 'analyze this code for bugs' --output-format json > analysis.json
這輸出包含成本和持續時間等元資料的 JSON 訊息陣列。
使用串流 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 支援自訂斜線命令,您可以建立這些命令來快速執行特定提示或任務。
有關更多詳細資訊,請參閱斜線命令 參考頁面。
建立專案特定命令
假設您想為您的專案建立可重複使用的斜線命令,所有團隊成員都可以使用。
在您的專案中建立命令目錄
mkdir -p .claude/commands
為每個命令建立 Markdown 檔案
echo "Analyze the performance of this code and suggest three specific optimizations:" > .claude/commands/optimize.md
提示:
命令名稱來自檔案名稱(例如,optimize.md 變成 /optimize)
您可以在子目錄中組織命令(例如,.claude/commands/frontend/component.md 建立 /component,在描述中顯示「(project:frontend)」)
專案命令對克隆儲存庫的每個人都可用
Markdown 檔案內容成為呼叫命令時發送給 Claude 的提示
使用 $ARGUMENTS 新增命令參數
假設您想建立可以接受使用者額外輸入的靈活斜線命令。
使用 $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
使用問題編號使用命令
在您的 Claude 會話中,使用帶有參數的命令。 這會在提示中將 $ARGUMENTS 替換為「123」。
提示:
$ARGUMENTS 佔位符會被命令後面的任何文字替換
您可以在命令範本中的任何位置放置 $ARGUMENTS
其他有用的應用:為特定函數產生測試案例、為組件建立文件、審查特定檔案中的程式碼,或將內容翻譯成指定語言
建立個人斜線命令
假設您想建立在所有專案中都有效的個人斜線命令。
在您的主資料夾中建立命令目錄
mkdir -p ~/.claude/commands
為每個命令建立 Markdown 檔案
echo "Review this code for security vulnerabilities, focusing on:" >
~ /.claude/commands/security-review.md
提示:
個人命令在使用 /help 列出時在其描述中顯示「(user)」
個人命令只對您可用,不與您的團隊共享
個人命令在您的所有專案中都有效
您可以將這些用於跨不同程式碼庫的一致工作流程
詢問 Claude 關於其功能
Claude 內建存取其文件,可以回答關於自身功能和限制的問題。
範例問題
> can Claude Code create pull requests?
> how does Claude Code handle permissions?
> what slash commands are available?
> how do I use MCP with Claude Code?
> how do I configure Claude Code for Amazon Bedrock?
> what are the limitations of Claude Code?
Claude 為這些問題提供基於文件的答案。對於可執行範例和實際演示,請參考上面的特定工作流程部分。
提示:
無論您使用的版本如何,Claude 始終可以存取最新的 Claude Code 文件
提出具體問題以獲得詳細答案
Claude 可以解釋複雜功能,如 MCP 整合、企業配置和進階工作流程
下一步
Claude Code 參考實作 克隆我們的開發容器參考實作。