Ignore a folder only on top level of a repository

I want to exclude a folder named assets on top level of my git-repository. Other folders in subdirectories having the name assets/ should not be excluded, e.g /extension/assets/*)

The folder /assets was in the repo until now.

I created a file .gitignore on top level and wrote /assets/* into this file.

Now I called git add . and made a commit: git commit -m testcommit then git clean -in and then git clean -i
I would have thought, that the content of the folder /assets/* now should get removed. But nothing happens.

Calling git ls-files | grep assets shows me, that all /assets/* directories are still present.

Did I get something wrong?

gitignore rules apply only to untracked files. If you want to stop tracking those files, you have to explicitly remove them from the repository. If you want to keep the files on disk (in your working tree) but remove them from the repository you can use git rm --cached.

As a side note, /assets/ would be a better rule, it ignores the directory and everything in it. It only makes a difference if you have files starting with names starting with a dot in the directory, though.

1 Like

Thanks for the clarification.

The command git rm -r --cached assets/* did the job.

1 Like