Question
How can I exclude .DS_Store or other files generated by the OS or text editor when using dev containers?
I am using Visual Studio Code Dev Containers.
The usual recommendation is to use the .gitignore of the repository only for the files and artifacts strictly related to that project.
For file exclusions related to the developer machine and environment (OS, text editor, etc.), the historical recommendation is to use ~/.gitignore_global (after enabling it with git config --global core.excludesFile ~/.gitignore_global). However this solution doesn’t work when working with dev containers in VS Code.
Answer
VS Code automatically copies the ~/.gitconfig from the developer machine to the dev container. However it doesn’t copy the files, like ~/.gitignore_global, referenced inside ~/.gitconfig.
We have recently opened an issue about that.
You could try with a file mount, but that creates other issues, for example:
- file missing in the developer machine and Docker creating an empty dir instead
- the fact that, even if the file is copied, the settings set by
~/.gitconfigmay point to a file path that doesn’t exist in the container (e.g./Users/someone/.gitignore_global).
Currently, if you don’t want to pollute the project .gitignore with developer-specific files generated by their OS or text editor, the best solution is to simply use .git/info/exclude.
For example, you can open your project directory in terminal and type this:
echo ".DS_Store" >> .git/info/exclude
This works properly inside the dev container because the project dir is already mounted inside the container.
It is also local to the developer machine and it is not tracked in Git by default, so you don’t pollute the project .gitignore.
Finally this solution work for any editor, not just VS Code. The solution is persistent and you don’t need to configure it again when you rebuild the container. The only downside, compared to a global .gitignore is that you need to type that command for every project. But this still seems the easiest solution right now.