I removed a folder from my git repo by mistake with git rm
commands.
Now, whatever modification I make in that fold will not show up in git status
How can I make git track this missing folder again? Thanks.

- 421
- 6
- 17
-
If you haven't committed the effects of `git rm that/dir/` : run `git restore -s HEAD -SW that/dir/` – LeGEC Aug 01 '22 at 06:32
-
Note that Git never stores any folders at all, and therefore doesn't track them either (actually it's kind of the other way around: it's unable to track them and therefore doesn't store them). The only thing Git stores are *files* and Git will create folder names on the fly as needed. That is, a commit might have `path/to/file` stored in it as a *file name* (complete with forward slashes); Git will make `path` and `to` if/when necessary to store `file` in `to` in `path` as your OS requires. – torek Aug 01 '22 at 10:36
-
None of this matters too much as long as there's at least one file in `path/to` so that you can `git add path/to/file`. It's when `path/to` exists, but is empty, that this shows up: since Git only stores *files* you have to create at least one file in there, such as `.gitignore`, so that you can `git add path/to/.gitignore` for instance. (Some people use `.gitkeep` as a fake file here, but I prefer `.gitignore`.) – torek Aug 01 '22 at 10:39
1 Answers
git rm
alone would not prevent git status to show differences in the folder.
Check if you have a .gitignore
rule which would explain why git status does not work in that folder.
git check-ignore -v -- yourFolder/aFile_inside_that_folder
If it displays a .gitignore
filename and a line, you will know why you don't see anything.
You can then delete that rule, and git status
should work again.
From the OP Jim Wang's comment
I tried the command you provided, and the output is:
fatal: Pathspec 'myFolder/setup.sh' is in submodule 'higherleveFolder/myFolder'.
At the very beginning, "
myFolder
" is a separate repo I cloned inside the original repo.
Could you tell me how to clean up everything and make git track "myFolder
" again?
If you have a .gitmodules
in the main repository root folder, try what I recommended in 2013:
0. mv a/submodule a/submodule_tmp
1. git submodule deinit -f -- higherleveFolder/myFolder
2. rm -rf .git/modules/higherleveFolder/myFolder
3. git rm --cached higherleveFolder/myFolder
# Note: higherleveFolder/myFolder (no trailing slash)
From there, you can git add
, commit
and push
higherleveFolder/myFolder
content.

- 1,262,500
- 529
- 4,410
- 5,250
-
Thanks for your guidance. I forgot what operations I did (i.e. git rm and others) to make git stop tracking this folder. I tried the command you provided, and the output is: fatal: Pathspec 'myFolder/setup.sh' is in submodule 'higherleveFolder/myFolder'. At the very beginning, "myFolder" is a separate repo I cloned inside the original repo. I am not an expert of Git but I tried a lot of random operations which caused a mess in my repo ... Could you tell me how to clean up everything and make git track "myFolder" again? Thanks. – Jim Wang Aug 02 '22 at 18:18
-
@JimWang Thank you for your feedback. I have edited the answer to address your comment. – VonC Aug 02 '22 at 18:52