DEV Community

Fredrik Sjöstrand
Fredrik Sjöstrand

Posted on • Edited on • Originally published at fronkan.hashnode.dev

A Personal .gitignore - Even for a Single Repository

I think I might be a bit messy, but maybe so are you? Either way, I quite often find myself with stuff in a repository that I don't want to delete or commit. Then the question becomes, should I add this to the .gitignore file? Quite often I feel like i shouldn't, as no one else working on the project will benefit from it. If everyone is adding their personal stuff to the file, it will get messy quite fast. What I want is a personal .gitignore. Does this exist? You bet it does!

Solution 1 - Configuring a Global .gitignore

One solution I have seen suggested is to use a global .gitignore. You configure this by setting the path in your global git config like this:

$ git config --global core.excludesFile ~/.gitignore
Enter fullscreen mode Exit fullscreen mode

Here I use a file named .gitignore in my home folder, but this could be any path.

My main issue with this solution is that this applies to all repositories. This can be useful if you for example always use the same name for your python virtual environments. However, quite often I find myself with some files that are relevant for a single project, making a global solution the wrong choice.

Solution 2 - Configuring a Local .gitignore

The observant reader might have noticed we used the --global flag with the command. May we not just change this to --local in the previous command? Yes, yes we can! This allows us to set the configuration for another .gitignore file for the single repository. To not have a naming collision we will need to name it something else, e.g. .mygitignore. The command looks like this:

$ git config --local core.excludesFile .mygitignore
Enter fullscreen mode Exit fullscreen mode

This will look for a file named .mygitignore in the current repository. Now you just need to create the file and add what you want to ignore to it. Here I would also suggest adding the .mygitignore file to itself. It will still be used but won't show up when you do e.g. git status

Sidenote: The local configuration file is stored in the .git/config in your repository if you ever need to change something.

Solution 3 - .git/info/exclude

You can also cut the middle man from the previous solution. Git already has a local .gitignore type file inside it that you can use.
This is the file .git/info/exclude. It uses the same syntax as your bog-standard .gitignore and is automatically used by .git, no need for any configuration. It doesn't get committed and is specific to the repository.

Closing Remarks

Of course, you should be careful that this doesn't result in duplicate work for your teammates. If everyone has a notebooks folder or names their virtual environments .venv, it is probably better to just add it to the .gitignore.

I'm torn between the second and third solutions. With the third solution, I like that it's standardized in git and doesn't require configuration. However, it hiding out in the .git folder makes it harder to remember when returning to an old project. In general, I never look in the .git folder unless I have to. So, I will go with the second solution. But, pick your poison as they say.

Top comments (0)