Git: fatal: Pathspec is in submodule
Asked Answered
D

6

199

I'm trying to get TravisCI to automatically deploy my Hakyll static site, according to this guide.

Here's how my repo is set up. I have my source branch, which contains my hakyll and markdown files. It builds the html into the _site directory, which is set up as a submodule, linked to my master branch.

I build the site without problem, then cd into the _site directory. However, when I try to git add ./* the newly generated HTML files, I get the following error:

fatal: Pathspec './about.html' is in submodule '_site'

When I try git add --all, I get this error:

git: pathspec.c:317: prefix_pathspec: Assertion `item->nowildcard_len <= item->len && item->prefix <= item->len' failed.

/home/travis/build.sh: line 245: 1566 Aborted git add --all

What is causing this, and how can I avoid this?

You can view the repository here.

Diarist answered 29/6, 2014 at 2:15 Comment(1)
it might be duplicated of #25458806 be aware the could be a problem of permissions on files https://mcmap.net/q/19900/-git-rm-fatal-pathspec-did-not-match-any-filesHardie
E
582

Removing the directory from git and adding it again worked for me:

 git rm --cached directory
 git add directory

This works if you purposefully removed the .git directory because you wanted to add directory to your main git project. In my specific case, I had git cloned an extension and ran git add . without thinking too much. Git decided to create a submodule, which I didn't like. So I removed directory/.git and ran into Git: fatal: Pathspec is in submodule. I couldn't find out how to remove the submodule stuff. Fixed with the two lines above.

Ecker answered 7/7, 2015 at 14:9 Comment(5)
This solved an issue I had where I accidentally pulled down a project into an existing project, creating a submodule. I had tried just about every other suggestion out there. Removing the directory and adding again addressed my problem. Thanks.Altair
I have .git and see it! But when I want to commit some files that located in a folder, it shows an error "blahblah did not match any file(s) known to git"Souvenir
This fixed the issue. In my case, I cloned the project in another machine.Mormon
I was having the same problem with a Visual Studio project which I synch with GitHub. The folder where most of my development is taking place wasn't being tracked, and if I tried adding it I was getting the same error. This solution fixed it for me too!Scauper
THANK. YOU!!! Took me two days to figure out why my CI/CD build was failing...Roughrider
B
18

I wanted to make a subdirectory stop being a git submodule. This worked for me:

$ mv subdir subdir2
$ git rm --cached subdir
$ mv subdir2 subdir
Begum answered 13/2, 2021 at 6:45 Comment(3)
Good hack :) Works.Onwards
I tried removing the .git and .gitignore files in the subdirectory, but it turned out I needed to remove the cache too. Worked, thanks!Candancecandela
I tried without rename and it still worked, thanks.Intramundane
T
10

It seems the git add context is the parent repo ("parent" means the one including the submodule), which triggers the warning.

Try and change its context with:

cd _site
git --git-dir=.git --work-tree=. add . 
git --git-dir=.git --work-tree=. commit -m "new files"

Don't forget that, if this works, you would still have to go back to the parent repo, and git add _site, since the subrepo would have changes.

And you would have to push both.


Update January 2017 (2+ years later)

With Git 2.12, you won't see that prefix_pathspec: Assertion anymore.

See commit 2d81c48 (09 Jan 2017) by Stefan Beller (stefanbeller).
Helped-by: Jeff King (peff), and Junio C Hamano (gitster).
(Merged by Junio C Hamano -- gitster -- in commit 00880a1, 18 Jan 2017)

pathspec: give better message for submodule related pathspec error

Running "git add a/b" when "a" is a submodule correctly errored out, but without a meaningful error message.

Toxicogenic answered 29/6, 2014 at 3:58 Comment(3)
I get error: unknown option git-dir=.git'`. Also, I can't commit my parent repo from within Travis, that will cause an infinite loop of Travis running...Diarist
@jmite I have fixed the answer: the --git-dir and --work-tree are options of the git command, not the git add command. Try at least the git add part, if the git commit isn't practical.Toxicogenic
This seems to break something for me, but I was able to find the solution hereAntonantone
H
6

It sounds like you're operating on non-initialized submodules (they're basically missing .git directories), therefore you should initialize them first and update:

git submodule init
git submodule update

Otherwise if you don't need this submodule anymore, remove it by:

git submodule deinit _site

or:

git rm -f --cached _site

and add it again:

git add _site

Check your current outstanding submodules by: git submodule status.

See also: Why is git erroring with 'Assertion failed' on git add .?

Hooch answered 30/12, 2016 at 0:33 Comment(0)
D
5

It seems my problem is that I was accidentally deleting the .git folder of the submodule.

Diarist answered 3/7, 2014 at 17:7 Comment(4)
I have exactly the same problem. Could you share how you solved it?Livelihood
I solved it by changing my script to not delete the .git folder. So I'd advise that you check your script, see if you actually have a .git folder in the directory.Diarist
I do but I want to get rid of it as remote repo no longer exists. It's just easier for us to include its source code as it is.Livelihood
You're best bet then is to look at changing the remote repos. Take a look at the git remote command.Diarist
E
2

100% fix for this problem, even if you have more than one submodule directory inside the project:

> git submodule foreach --recursive deinit -f --all -- <relative path>
> git add --all -f
Elasticity answered 24/2, 2019 at 16:10 Comment(1)
deinit learned something new.Intramundane

© 2022 - 2024 — McMap. All rights reserved.