Reflect on your 2024 year in code

"Did not match any file(s) known to git"

Greg Foster
Greg Foster
Graphite software engineer
Try Graphite


Note

This guide explains this concept in vanilla Git. For Graphite documentation, see our CLI docs.


This error occurs when you try to perform a Git operation (e.g., checkout, add, or commit) and the file or path you specified doesn't match any files that Git is tracking. It can happen when attempting to run git checkout, and it can stem from:

  • Typographical errors in the file or directory name.
  • Case sensitivity differences in file names.
  • Ignored files due to .gitignore rules.
  • Trying to reference a file or branch that doesn't exist in the repository.

git fetch

This synchronizes branch information between your local development environment and remote. It does not attempt to merge remote changes into local. After that, you should be able to run git checkout again.

Git is precise about file and directory names. Even a minor typo will result in this error.

  1. Use ls (list files) or dir (Windows) in your terminal to verify the correct file path:
    Terminal
    ls path/to/directory/
  2. Confirm the file exists and the path is correct.
  3. Retype the command with the verified path:
    Terminal
    git add path/to/your-file.txt

Git is case-sensitive by default, even on case-insensitive file systems (e.g., Windows or macOS).

If your file is named ReadMe.md and you run:

Terminal
git add readme.md

You’ll encounter the error.

  1. Use ls to check the file's exact case:
    Terminal
    ls -l
  2. Use the exact case in your Git commands:
    Terminal
    git add ReadMe.md

Files listed in .gitignore are excluded from Git operations.

  1. Check if the file is ignored:
    Terminal
    git check-ignore -v path/to/your-file.txt
    If it is ignored, the output will show the .gitignore rule causing the exclusion.
  2. Modify .gitignore to allow the file:
    • Remove the specific rule, or
    • Add an exception:
      Terminal
      !path/to/your-file.txt
  3. Stage the file:
    Terminal
    git add path/to/your-file.txt

If you're trying to checkout a branch or commit, the error indicates it doesn’t exist or isn’t reachable.

You attempt:

Terminal
git checkout feature/nonexistent-branch
  1. List all available branches:
    Terminal
    git branch -a
    Look for the branch you want. If it exists, use the exact name:
    Terminal
    git checkout feature/existing-branch
  2. For commits, ensure you’re using a valid hash:
    Terminal
    git checkout <valid-commit-hash>

Git operations like checkout only apply to tracked files. If the file was never added, Git doesn’t know about it.

  1. Check if the file is tracked:
    Terminal
    git ls-files | grep your-file.txt
    If there’s no output, the file is not tracked.
  2. Stage the file:
    Terminal
    git add path/to/your-file.txt
  3. Commit the file:
    Terminal
    git commit -m "Add your file"

File names with special characters, spaces, or non-ASCII characters can cause issues.

A file named my file.txt will need escaping or quoting:

Terminal
git add "my file.txt"
  1. Use quotes or escape spaces:
    Terminal
    git add "path/to/my file.txt"
    or
    Terminal
    git add path/to/my\ file.txt

When troubleshooting, use these commands to gather insights:

  1. verify the current branch:
    Terminal
    git branch
  2. list all files in the repository:
    Terminal
    git ls-files
  3. inspect the .gitignore rules:
    Terminal
    cat .gitignore
  4. check for ignored files:
    Terminal
    git check-ignore -v *

By following these steps, you can effectively resolve the "did not match any file(s) known to git" error and avoid it in future Git operations.

Git inspired
Graphite's CLI and VS Code extension make working with Git effortless.
Learn more

Built for the world's fastest engineering teams, now available for everyone