18 KiB
Common workflows
Learn about common workflows with Claude Code.
Each task in this document includes clear instructions, example commands, and best practices to help you get the most from Claude Code.
Understand new codebases
Get a quick codebase overview
Suppose you've just joined a new project and need to understand its structure quickly.
```bash cd /path/to/project ``` ```bash 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?
```
- Start with broad questions, then narrow down to specific areas
- Ask about coding conventions and patterns used in the project
- Request a glossary of project-specific terms
Find relevant code
Suppose you need to locate code related to a specific feature or functionality.
``` > find the files that handle user authentication ``` ``` > how do these authentication files work together? ``` ``` > trace the login process from front-end to database ``` Tips:- Be specific about what you're looking for
- Use domain language from the project
Fix bugs efficiently
Suppose you've encountered an error message and need to find and fix its source.
``` > 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 ``` Tips:- Tell Claude the command to reproduce the issue and get a stack trace
- Mention any steps to reproduce the error
- Let Claude know if the error is intermittent or consistent
Refactor code
Suppose you need to update old code to use modern patterns and practices.
``` > 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 ``` Tips:- Ask Claude to explain the benefits of the modern approach
- Request that changes maintain backward compatibility when needed
- Do refactoring in small, testable increments
Work with tests
Suppose you need to add tests for uncovered code.
``` > 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 ``` Tips:- Ask for tests that cover edge cases and error conditions
- Request both unit and integration tests when appropriate
- Have Claude explain the testing strategy
Create pull requests
Suppose you need to create a well-documented pull request for your changes.
``` > summarize the changes I've made to the authentication module ``` ``` > create a pr ``` ``` > enhance the PR description with more context about the security improvements ``` ``` > add information about how these changes were tested ``` Tips:- Ask Claude directly to make a PR for you
- Review Claude's generated PR before submitting
- Ask Claude to highlight potential risks or considerations
Handle documentation
Suppose you need to add or update documentation for your code.
``` > 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 ``` Tips:- Specify the documentation style you want (JSDoc, docstrings, etc.)
- Ask for examples in the documentation
- Request documentation for public APIs, interfaces, and complex logic
Use extended thinking
Suppose you're working on complex architectural decisions, challenging bugs, or planning multi-step implementations that require deep reasoning.
``` > 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 will gather relevant information from your codebase and
use extended thinking, which will be visible in the interface.
```
> think harder about edge cases we should handle
```
Extended thinking is most valuable for complex tasks such as:
- Planning complex architectural changes
- Debugging intricate issues
- Creating implementation plans for new features
- Understanding complex codebases
- Evaluating tradeoffs between different approaches
The way you prompt for thinking results in varying levels of thinking depth:
- "think" triggers basic extended thinking
- intensifying phrases such as "think more", "think a lot", "think harder", or "think longer" triggers deeper thinking
For more extended thinking prompting tips, see Extended thinking tips.
Claude will display its thinking process as italic gray text above the response.Resume previous conversations
Suppose you've been working on a task with Claude Code and need to continue where you left off in a later session.
Claude Code provides two options for resuming previous conversations:
--continueto automatically continue the most recent conversation--resumeto display a conversation picker
This immediately resumes your most recent conversation without any prompts.
Use `--print` with `--continue` to resume the most recent conversation in non-interactive mode, perfect for scripts or automation.
This displays an interactive conversation selector showing:
* Conversation start time
* Initial prompt or conversation summary
* Message count
Use arrow keys to navigate and press Enter to select a conversation.
- Conversation history is stored locally on your machine
- Use
--continuefor quick access to your most recent conversation - Use
--resumewhen you need to select a specific past conversation - When resuming, you'll see the entire conversation history before continuing
- The resumed conversation starts with the same model and configuration as the original
How it works:
- Conversation Storage: All conversations are automatically saved locally with their full message history
- Message Deserialization: When resuming, the entire message history is restored to maintain context
- Tool State: Tool usage and results from the previous conversation are preserved
- Context Restoration: The conversation resumes with all previous context intact
Examples:
# Continue most recent conversation
claude --continue
# Continue most recent conversation with a specific prompt
claude --continue --print "Show me our progress"
# Show conversation picker
claude --resume
# Continue most recent conversation in non-interactive mode
claude --continue --print "Run the tests again"
Run parallel Claude Code sessions with Git worktrees
Suppose you need to work on multiple tasks simultaneously with complete code isolation between Claude Code instances.
Git worktrees allow you to check out multiple branches from the same repository into separate directories. Each worktree has its own working directory with isolated files, while sharing the same Git history. Learn more in the [official Git worktree documentation](https://git-scm.com/docs/git-worktree). ```bash # Create a new worktree with a new branch git worktree add ../project-feature-a -b feature-a# Or create a worktree with an existing branch
git worktree add ../project-bugfix bugfix-123
```
This creates a new directory with a separate working copy of your repository.
# Run Claude Code in this isolated environment
claude
```
# Remove a worktree when done
git worktree remove ../project-feature-a
```
- Each worktree has its own independent file state, making it perfect for parallel Claude Code sessions
- Changes made in one worktree won't affect others, preventing Claude instances from interfering with each other
- All worktrees share the same Git history and remote connections
- For long-running tasks, you can have Claude working in one worktree while you continue development in another
- Use descriptive directory names to easily identify which task each worktree is for
- Remember to initialize your development environment in each new worktree according to your project's setup. Depending on your stack, this might include:
- JavaScript projects: Running dependency installation (
npm install,yarn) - Python projects: Setting up virtual environments or installing with package managers
- Other languages: Following your project's standard setup process
- JavaScript projects: Running dependency installation (
Use Claude as a unix-style utility
Add Claude to your verification process
Suppose you want to use Claude Code as a linter or code reviewer.
Add Claude to your build script:
// 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.'"
}
}
- Use Claude for automated code review in your CI/CD pipeline
- Customize the prompt to check for specific issues relevant to your project
- Consider creating multiple scripts for different types of verification
Pipe in, pipe out
Suppose you want to pipe data into Claude, and get back data in a structured format.
Pipe data through Claude:
cat build-error.txt | claude -p 'concisely explain the root cause of this build error' > output.txt
- Use pipes to integrate Claude into existing shell scripts
- Combine with other Unix tools for powerful workflows
- Consider using --output-format for structured output
Control output format
Suppose you need Claude's output in a specific format, especially when integrating Claude Code into scripts or other tools.
```bash cat data.txt | claude -p 'summarize this data' --output-format text > summary.txt ```This outputs just Claude's plain text response (default behavior).
This outputs a JSON array of messages with metadata including cost and duration.
This outputs a series of JSON objects in real-time as Claude processes the request. Each message is a valid JSON object, but the entire output is not valid JSON if concatenated.
- Use
--output-format textfor simple integrations where you just need Claude's response - Use
--output-format jsonwhen you need the full conversation log - Use
--output-format stream-jsonfor real-time output of each conversation turn
Create custom slash commands
Claude Code supports custom slash commands that you can create to quickly execute specific prompts or tasks.
For more details, see the Slash commands reference page.
Create project-specific commands
Suppose you want to create reusable slash commands for your project that all team members can use.
```bash mkdir -p .claude/commands ``` ```bash echo "Analyze the performance of this code and suggest three specific optimizations:" > .claude/commands/optimize.md ``` ``` > /project:optimize ``` Tips:- Command names are derived from the filename (e.g.,
optimize.mdbecomes/project:optimize) - You can organize commands in subdirectories (e.g.,
.claude/commands/frontend/component.mdbecomes/project:frontend:component) - Project commands are available to everyone who clones the repository
- The Markdown file content becomes the prompt sent to Claude when the command is invoked
Add command arguments with $ARGUMENTS
Suppose you want to create flexible slash commands that can accept additional input from users.
```bash 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 ``` In your Claude session, use the command with arguments.```
> /project:fix-issue 123
```
This will replace \$ARGUMENTS with "123" in the prompt.
- The $ARGUMENTS placeholder is replaced with any text that follows the command
- You can position $ARGUMENTS anywhere in your command template
- Other useful applications: generating test cases for specific functions, creating documentation for components, reviewing code in particular files, or translating content to specified languages
Create personal slash commands
Suppose you want to create personal slash commands that work across all your projects.
```bash mkdir -p ~/.claude/commands ``` ```bash echo "Review this code for security vulnerabilities, focusing on:" > ~/.claude/commands/security-review.md ``` ``` > /user:security-review ``` Tips:- Personal commands are prefixed with
/user:instead of/project: - Personal commands are only available to you and not shared with your team
- Personal commands work across all your projects
- You can use these for consistent workflows across different codebases