gitgitignoregit-statusgit-untrackedgit-subrepo

How do I remove untracked directory tree (submodule / sub-repository) from main git repository?


For the life of me I can't seem to 'remove' an untracked sub-module/directory from the main git repository. I've tried various syntaxes, but I keep getting the 'pathspec' error stating there was no match, even though the folder is there in plain site. I added it to the .gitignore and it has not been added or committed to the main repository at all yet, but it keeps showing up in red under Untracked files: when I run git status and I can't get rid of it. git clean -n shows nothing either.

Any clues as to what I'm doing wrong here?

To be clear: I don't want to delete the sub-repo, I merely want to track the sub-repo entirely separately and have the main-repo totally ignore it as if it does not exist.

### /home/user/  <-- main/master repository (ACTUAL), separate git-directory

$pwd
/home/user


$cat .git
gitdir: /home/git-repositories/user-main


### /home/user/ntc-templates/   <-- sub repository, separate git-directory

$cat ntc-templates/.git
gitdir: /home/git-repositories/ntc-templates


$git status
On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
    modified:   .recently-used

Untracked files:
  (use "git add <file>..." to include in what will be committed)
    ntc-templates/     ### <<------------------ how do I get rid of this line?

no changes added to commit (use "git add" and/or "git commit -a")


$git rm -rf ntc-templates/
fatal: pathspec 'ntc-templates/' did not match any files


$git rm -rfn --cached ntc-templates/
fatal: pathspec 'ntc-templates/' did not match any files


$git rm -rfn --cached ntc-templates
fatal: pathspec 'ntc-templates' did not match any files


$git rm -rfn --cached ~/ntc-templates
fatal: pathspec '/home/user/ntc-templates' did not match any files


$ll ntc-templates/
total 168
drwxr-xr-x  6 user user  4096 Feb 10 11:49 ./
drwxr-xr-x 49 user user  4096 Feb 10 11:59 ../
-rw-r--r--  1 user user 29276 Feb 10 11:48 CHANGELOG
-rw-r--r--  1 user user 15443 Feb 10 11:48 development_scripts.py
-rw-r--r--  1 root root    45 Feb 10 11:49 .git
drwxr-xr-x  2 user user  4096 Feb 10 11:48 .github/
-rw-r--r--  1 user user    54 Feb 10 11:48 .github_changelog_generator
-rw-r--r--  1 user user  2622 Feb 10 11:48 .gitignore
drwxr-xr-x  3 user user  4096 Feb 10 11:48 lib/
-rw-r--r--  1 user user   601 Feb 10 11:48 LICENSE
-rw-r--r--  1 user user    29 Feb 10 11:48 MANIFEST.in
-rw-r--r--  1 user user   232 Feb 10 11:48 pyproject.toml
-rw-r--r--  1 user user 22240 Feb 10 11:48 README.md
-rw-r--r--  1 user user  1412 Feb 10 11:48 setup.py
drwxr-xr-x  2 user user 36864 Feb 10 11:48 templates/
drwxr-xr-x 37 user user  4096 Feb 10 11:48 tests/
-rw-r--r--  1 user user   243 Feb 10 11:48 tox.ini
-rw-r--r--  1 user user   101 Feb 10 11:48 .travis.yml
-rw-r--r--  1 user user   703 Feb 10 11:48 .yamllint


$git-ignore | grep ntc
ntc-templates/**
ntc-templates/CHANGELOG
ntc-templates/development_scripts.py
ntc-templates/lib
ntc-templates/LICENSE
ntc-templates/MANIFEST.in
ntc-templates/pyproject.toml
ntc-templates/README.md
ntc-templates/setup.py
ntc-templates/templates
ntc-templates/tests
ntc-templates/tox.ini


$

Thanks.


Solution

  • Untracked files are files/directories on disk, which aren't tracked yet by git, and which aren't explicitly ignored either.

    You can either tell git to ignore it, or remove that file/directory from disk :

    # option 1 : ignore that directory
    $ echo "nc-templates" >> .gitignore
    $ git add .gitignore
    $ git commit
    
    # I think this is *not* what you want, but for completeness :
    # option 2 : remove the directory
    $ rm -rf nc-templates
    

    note : there are actually several ways to ignore a file/directory, see for example :

    When would you use .git/info/exclude instead of .gitignore to exclude files?


    [edit]
    why ntc-templates/** in .gitignore "doesn't work" :

    ntc-templates/ is itself a git repo, so it is treated as a potential submodule. The parent git repo would not look at files under ntc-templates/ (they would be handled by the subrepo itself), so patterns digging below ntc-templates/ (like ntc-templates/**) do not apply.