File Operations
Safe, efficient file manipulation with Claude Code. Creating, editing, and managing files without the risk.
Premium Course Content
This lesson is part of a premium course. Upgrade to Pro to unlock all premium courses and content.
- Access all premium courses
- 1000+ AI skills included
- New content added weekly
Claude Can Change Your Files
In the previous lesson, we explored task orchestration. Now let’s build on that foundation. This is powerful. It’s also a responsibility.
Claude Code can create files, edit files, delete files. Done well, this is a huge productivity boost. Done carelessly, you can make a mess.
This lesson teaches you to work with files safely and efficiently.
The Safety Net: Git
Before any significant Claude work, make sure:
Clean working directory
git status # Should be clean or committedKnow what branch you’re on
git branch # Know where you areCommit often After good changes, commit. If things go wrong, you can always get back.
Git is your safety net. With it, any Claude mistake is recoverable.
File Operation Patterns
Creating New Files
> Create a new utility file at src/utils/string-helpers.js
> Include functions for: capitalize, truncate, slugify
Claude creates the file with content. Review before moving on.
For multiple related files:
> Create the following files for a new "comments" feature:
> - src/models/comment.js (data model)
> - src/services/comment-service.js (business logic)
> - src/routes/comments.js (API routes)
> - tests/comments.test.js (test skeleton)
Related files created together tend to be consistent.
Editing Existing Files
Always add the file first:
/add src/auth/login.js
> Add rate limiting to the login function. Max 5 attempts per minute.
Claude sees the file, understands the context, makes targeted changes.
Be specific about what to change:
> In the handleSubmit function, add validation before the API call.
> Check that email is valid format and password is at least 8 characters.
Specific instructions → precise changes.
Multi-File Edits
For coordinated changes across files:
/add src/types/user.ts src/services/user-service.ts src/routes/users.ts
> Add an optional "phoneNumber" field to the User type.
> Update the service and routes to handle it.
All relevant files in context → consistent changes across them.
Renaming and Moving
> Rename src/utils/helpers.js to src/utils/string-utils.js
> Update all import statements across the codebase.
Claude handles both the rename and the import updates.
Deleting Files
> The src/old-auth/ directory is no longer used.
> Remove it and clean up any references.
Be explicit about what to delete. Claude will ask for confirmation on destructive operations.
Review Strategies
Inline Review
After each change, review before continuing:
> Add input validation to the createUser function
# Claude makes changes
> Show me the diff
# Review the changes
> Good. Now add tests for the validation.
Git Diff Review
After Claude makes changes:
Quick check: Before moving on, can you recall the key concept we just covered? Try to explain it in your own words before continuing.
git diff src/auth/login.js # See exactly what changed
Git diff is often clearer than inline review for complex changes.
Staged Review
For large changes:
> Make changes to implement feature X
# Claude makes many changes
git add -p # Interactively stage changes
git commit -m "Part 1: model changes"
# Continue staging
git add -p
git commit -m "Part 2: service layer"
Review and commit in logical chunks.
Recovery Procedures
/undo
Immediately undo Claude’s last change:
/undo
Use this when you see an unwanted change. Works best before you’ve continued.
Git Reset
If you haven’t committed:
git checkout -- src/file-that-got-messed-up.js
Restores the file to last committed state.
Stash and Start Over
If things are messy:
git stash # Save current changes
# Start fresh with Claude
git stash pop # Bring back changes if needed
Full Reset
Nuclear option (uncommitted changes only):
git reset --hard HEAD
Back to last commit. Use sparingly.
Efficiency Techniques
Batch Related Changes
Slow:
> Add field "createdAt" to User model
> Now add field "updatedAt" to User model
> Now add field "lastLoginAt" to User model
Fast:
> Add timestamp fields to User model:
> - createdAt: when user was created
> - updatedAt: when user was last modified
> - lastLoginAt: when user last logged in
One request for related changes.
Template-Based Creation
> Create 5 new API route files following the pattern of src/routes/users.js:
> - products.js
> - orders.js
> - inventory.js
> - categories.js
> - suppliers.js
Claude replicates patterns efficiently.
Bulk Operations
> In all files matching src/services/*.js:
> - Add error logging to catch blocks
> - Use the logger from src/utils/logger.js
Pattern-based bulk changes.
File Operation Checklist
Before file operations:
- Git working directory is clean (or changes are stashed)
- Relevant files are added to context
- Task is specific enough to get right changes
During file operations:
- Review changes before continuing
- Use
/undoimmediately if something’s wrong - Commit logical groups of changes
After file operations:
- Review full diff with
git diff - Run tests if applicable
- Commit with clear message
Key Takeaways
- Git is your safety net—commit often, revert easily
- Always add files to context before requesting changes
- Be specific about what to change and where
- Review changes before continuing
- Use
/undoand git for recovery - Batch related changes for efficiency
Next: integrating Bash commands for powerful automation.
Up next: In the next lesson, we’ll dive into Bash Integration.
Knowledge Check
Complete the quiz above first
Lesson completed!