Claude Code keeps running commands in the wrong directory. How do I stop it?
Claude Code often runs cd, make, git add, or file reads from the wrong directory and retries many times before noticing. One user lost work after a git hard reset ran in the wrong directory. How can you keep Claude anchored to the right folder?
Originally asked on GitHub: https://github.com/anthropics/claude-code/issues/1669
5 answers
Status: open. The issue is still open and the thread does not state a maintainer fix. Users reported these workarounds.
What helped in the thread:
- Show the working directory at all times. One user wrote a status line script that reads
workspace.current_dirfrom the JSON Claude Code passes on stdin, extracts it with jq, and prints it next to the model name and git branch. - Put the directory layout in the project's markdown memory file. One user listed the project root, the working directory, and key subdirectories as absolute paths, and told Claude to check the project root with
../filenamebefore assuming a file is missing. They noted this stopped working after context compaction, so they had to ask Claude to re-read the file. - Use a guard hook that blocks directory changes. A commenter linked a set of hooks from the lpclaude repository. Another commenter asked whether both hooks were needed, and the reply was that guard-cd is all that is needed.
General advice: keep destructive commands such as
git reset --hardandrm -rfout of automatic approval, and review commands that use relative paths before approving them.Did this work for you?- Show the working directory at all times. One user wrote a status line script that reads
What a GitHub user tried on GitHub (Outcome unclear)
Reporter asked for the tool to prepend the current working directory to every prompt, after a git hard reset ran in the wrong directory.
Source: https://github.com/anthropics/claude-code/issues/1669 (opens external site)
Did this work for you?What @ankuranandredelk tried on GitHub (Partly worked)
Added a Directory Navigation Guidelines section to the project's markdown file listing the project root, working directory, and key paths as absolute paths, and told Claude to check the project root before assuming a file is missing.
Source: https://github.com/anthropics/claude-code/issues/1669#issuecomment-3167301608 (opens external site)
Did this work for you?What @bradleyjames tried on GitHub (Partly worked)
Built a status line script that reads workspace.current_dir from the JSON input with jq and shows it with the git branch, so the directory is always visible.
Source: https://github.com/anthropics/claude-code/issues/1669#issuecomment-3272495973 (opens external site)
Did this work for you?What @lpasqualis tried on GitHub (Worked)
Installed the hooks from the lpclaude repository to guard against directory changes. A later commenter said guard-cd is the hook that is needed.
Source: https://github.com/anthropics/claude-code/issues/1669#issuecomment-3272548126 (opens external site)
Did this work for you?