git error
pathspec issue
git submodule
debugging git
git troubleshooting

Git fatal Pathspec is in submodule

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

fatal: pathspec is in submodule means the path you passed to a Git command belongs to a nested Git repository, not to the parent repository’s normal tracked files. The fix is usually to run the command inside the submodule itself or to update the submodule pointer from the parent repo instead of trying to manipulate submodule contents directly at the top level.

Why Git Raises This Error

A submodule is stored in the parent repository as a gitlink, which is essentially a pointer to one commit of another repository. From the parent repo’s perspective, it does not track every file inside that nested project individually.

So if you run a command like this from the parent repository:

bash
git checkout HEAD -- vendor/lib/src/file.c

and vendor/lib is a submodule, Git cannot treat src/file.c as a normal parent-repo path. It belongs to the child repository.

Verify That The Path Is A Submodule

Start by confirming the submodule status.

bash
git submodule status
git ls-files --stage | grep vendor/lib

If the path is a submodule, you will see that the parent repository tracks the submodule directory as one entry rather than tracking the nested files individually.

Work Inside The Submodule

If you want to inspect, restore, or commit a file inside the submodule, change into that submodule and run Git there.

bash
cd vendor/lib
git status
git checkout HEAD -- src/file.c

That works because you are now operating in the repository that actually owns src/file.c.

Update The Parent Repo Correctly

If the submodule itself moved to a different commit and you want the parent repo to record that new submodule revision, the workflow is two-step:

  1. update the submodule repository
  2. commit the new gitlink in the parent repository
bash
1cd vendor/lib
2git checkout main
3git pull
4cd ..
5cd ..
6git add vendor/lib
7git commit -m "Update submodule pointer"

From the parent repository’s point of view, you are not adding changed files inside the submodule. You are adding a new submodule commit reference.

Restore A Submodule To The Recorded Commit

If your goal is to discard local submodule drift and return to the commit recorded by the parent repository, run:

bash
git submodule update --init --recursive

That resets the checked-out state of the submodule to the commit expected by the parent repository.

When The Error Appears During git add

A common mistake is trying to add a file under a submodule path from the parent repository.

bash
git add vendor/lib/src/file.c

This fails for the same reason: the parent repository does not own that file directly. Either add and commit inside the submodule, or add the submodule directory itself in the parent repository if the submodule commit changed.

Common Pitfalls

The most common mistake is forgetting that a submodule is a repository boundary. The parent repository tracks the submodule commit, not every file under it.

Another mistake is editing files inside a submodule and then expecting git status in the parent repo to explain the full nested change set. The parent usually only shows that the submodule has moved or is dirty.

A third issue is trying to fix the problem by removing the submodule directory manually. That usually creates a mess unless your real goal is to remove the submodule entirely.

Summary

  • The error means the specified path belongs to a submodule, not to the parent repository.
  • Run file-level Git commands inside the submodule itself.
  • Use git add submodule-path in the parent repo only to record a new submodule commit reference.
  • 'git submodule update --init --recursive restores submodules to the commits recorded by the parent.'
  • Treat submodules as repository boundaries when debugging Git pathspec errors.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.