0Pricing
Claude Architect · 课时

用户、项目与目录级别

了解每个 CLAUDE.md 所在的位置及其适用对象。

用户、项目与目录级别 是 CoddyKit 上的免费 Claude Architect 课时。 这是第 1 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Claude Architect 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Claude Architect 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

Three Places a CLAUDE.md Can Live

Claude Code reads memory from three levels, and each one answers a different question: who does this rule apply to?

  • User level — ~/.claude/CLAUDE.md: your personal preferences, on your machine only.
  • Project level — ./CLAUDE.md or .claude/CLAUDE.md: shared with the whole team via version control.
  • Directory level — a CLAUDE.md inside a subfolder: scoped to that subtree only.

Knowing which file a rule belongs in is a core Claude Certified Architect skill. Put it in the wrong place and either your teammates miss it, or your personal habits leak into everyone's workflow.

User Level — Personal, NOT Shared

The user-level file at ~/.claude/CLAUDE.md lives in your home directory. It applies to every project you open on this machine, but it travels with you, not the repo.

Critically, it is NOT shared via version control. A new teammate cloning the repo will never see it. So it is the right home for personal taste: your preferred commit-message style, how chatty you like responses, or shell aliases you favor.

It is the wrong home for anything the team must agree on — coding standards, build commands, security rules.

# ~/.claude/CLAUDE.md  (personal, machine-local, never committed)

## My preferences
- Keep explanations short; show the command, not a paragraph.
- Prefer `rg` over `grep` and `fd` over `find` on my machine.
- Use Conventional Commits for messages.

Project Level — Shared via VCS

The project-level file lives at the repo root as ./CLAUDE.md (or .claude/CLAUDE.md). Because it sits inside the repository, it is committed and shared via version control.

That is exactly what you want for rules the whole team relies on: the build command, test framework, architectural conventions, directory layout, and anything a new contributor needs on day one.

Rule of thumb: if a fresh clone would break without it, it belongs in the project-level CLAUDE.md — not in your personal user file.

# ./CLAUDE.md  (repo root — committed, shared by everyone)

## Build & Test
- Build:  yarn build
- Test:   yarn test --runInBand
- Lint must pass before every commit.

## Conventions
- NestJS modules under src/<feature>/.
- All DB access goes through the repository layer, never inline SQL.

Directory Level — Scoped to a Subtree

A CLAUDE.md placed inside a subfolder is directory-scoped: it applies only when Claude is working within that subtree. This keeps narrow, local rules close to the code they govern.

Example: a frontend/CLAUDE.md can hold React/Tailwind conventions that make no sense for the backend, while services/payments/CLAUDE.md can carry rules that only matter for billing code.

Directory-level files are still committed (they live in the repo), so they are shared — but their reach is the folder, not the whole project.

# frontend/CLAUDE.md  (scoped to the frontend subtree)

## UI conventions (apply only under frontend/)
- Components are function components with hooks; no class components.
- Style with Tailwind utility classes; avoid inline style objects.
- Co-locate tests as Component.test.tsx next to the component.

How the Levels Combine

The levels are additive context, not mutually exclusive. When you work in a subfolder, Claude can draw on the user file, the project file, and the directory file together.

Think of it as concentric scopes:

  • User — broadest reach (every project), narrowest audience (just you).
  • Project — whole repo, whole team.
  • Directory — one subtree, whole team.

So the question is never only "how specific is this rule?" but also "who needs to see it — me, or everyone?" Audience decides user vs project; scope decides project vs directory.

The Classic Mistake: Team Rules in the User File

The most common anti-pattern: putting a rule everyone depends on into ~/.claude/CLAUDE.md. It works perfectly for you — so the gap is invisible — but because the user file is never shared via VCS, new teammates miss it entirely.

Symptoms: Claude follows the convention on your machine and ignores it on a colleague's, and nobody can figure out why. The fix is always the same: move shared rules down into the project-level (or directory-level) CLAUDE.md so they ship with the repo.

Modularize with @path Imports

A CLAUDE.md does not have to be one giant file. You can pull in other files with @path imports, which keeps each file focused and readable.

For example, a project file can import a shared standards document instead of inlining hundreds of lines. This makes the hierarchy easier to maintain and lets you reuse the same standards across files.

# ./CLAUDE.md  (project root)

## Engineering standards
@./standards/coding-style.md
@./standards/security.md

## Build
- Build: yarn build

Path-Scoped Rules with .claude/rules/

A monolithic CLAUDE.md loads into context for every task — even when most of it is irrelevant — wasting tokens. A cleaner pattern for narrow rules is .claude/rules/ files with YAML frontmatter.

Each rule file declares a paths pattern, and it loads only when you are editing matching files. This gives you directory-level precision while saving context, because rules that don't match the current file are never pulled in.

# .claude/rules/migrations.md
---
paths:
  - "db/migrations/**"
---

- Every migration must be reversible (provide a down step).
- Never edit a migration that has already shipped; add a new one.

Editing Memory: /memory and /init

You don't have to hand-edit these files in an external editor. Inside Claude Code, the /memory command opens and edits the CLAUDE.md memory, and changes persist across sessions.

For a brand-new repository, /init bootstraps a project-level CLAUDE.md by documenting the codebase, giving you a sensible starting point you can then refine.

Choosing which file /memory writes to still comes back to the same decision: personal (user) versus shared (project or directory).

# In an interactive Claude Code session:
/init      # generate a project-level CLAUDE.md from the codebase
/memory    # open and edit CLAUDE.md; edits persist across sessions

Skills and Commands Follow the Same Split

This user-versus-project split is not unique to CLAUDE.md — it is a consistent Claude Code pattern. Skills and commands use the exact same two scopes:

  • Project scope — .claude/skills/ and .claude/commands/ in the repo are shared via VCS.
  • User scope — ~/.claude/skills/ is personal.

Note that .claude/commands/ is the legacy form and .claude/skills/ is the current one, but the scoping rule is identical: repo = team, home = you. Learn the split once and it applies everywhere.

Keep Secrets Out of Committed Files

Because project-level and directory-level files are committed to version control, treat them like public code: never put secrets in them. The same discipline applies to a project-scoped .mcp.json (shared in VCS) versus the personal ~/.claude.json.

For tokens and keys, reference environment variables — for example ${GITHUB_TOKEN} — instead of pasting the raw value. The shared file says which variable to use; the actual secret stays in the environment, out of the repo and out of CLAUDE.md.

// .mcp.json  (committed — references env vars, never raw secrets)
{
  "mcpServers": {
    "github": {
      "command": "github-mcp",
      "env": { "GITHUB_TOKEN": "${GITHUB_TOKEN}" }
    }
  }
}

Quick Check: Where Does the Rule Go?

Your team agrees on a build command and a test framework that every contributor must use. A new engineer just cloned the repo. Where should this rule live so that Claude Code applies it for the whole team from day one?

Recap: Audience and Scope Decide the Level

Pick the level by answering two questions — who and where:

  • User (~/.claude/CLAUDE.md): personal, every project, NOT shared via VCS — new teammates miss it. Use for personal preferences only.
  • Project (./CLAUDE.md): shared via VCS, whole team, whole repo. The home for build commands and conventions everyone needs.
  • Directory (subfolder CLAUDE.md): shared via VCS but scoped to one subtree.

Keep files lean with @path imports and load narrow rules on demand via .claude/rules/ with paths frontmatter. Never commit secrets — reference env vars like ${GITHUB_TOKEN}. The biggest trap to avoid: putting team rules in your personal user file, where everyone else will silently never see them.

常见问题解答

「用户、项目与目录级别」课时是免费的吗?

是的 — 「用户、项目与目录级别」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Claude Architect 课程的其余内容,请升级到 CoddyKit PRO。 Claude Architect 课程共包含 4 节课。

「用户、项目与目录级别」这节课中我会学到什么?

了解每个 CLAUDE.md 所在的位置及其适用对象。 你通过在浏览器中直接运行的动手代码来练习 Claude Architect,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Claude Architect 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Claude Architect 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 1 节课,共 4 节。

「用户、项目与目录级别」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Claude Architect 课中编写并运行代码吗?

能。每节 Claude Architect 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 用户、项目与目录级别
  2. @path 导入语法
  3. 带 Frontmatter 路径的 .claude/rules/
  4. 单体规则与模块化规则
← 返回 Claude Architect