How to create a local gitignore file

Dante De Ruwe • 2 min read • This article was written by a human

Why?

Sometimes, you want to experiment and tinker with stuff locally without git tracking your files. Think about experimenting with AI agents, local scripts, or even leaving yourself todo notes in every repo.

In stead of carefully avoiding to stage and commit these files, we can let git ignore them without changing the repo’s .gitignore!

Additional gitignore files

Did you know you can create additional gitignore files? These can extend your main gitignore file.

You can let git know it should treat an extra gitignore file with:

git config --local core.excludesfile "<my-gitignore-path>"

This way, you can add your experimental files or folders that you’re not ready for the world to see, away from the allseeing eye of git 👀

Keeping your gitignore file local

We don’t want this gitignore to be added to the repo! So we’ll have to set it up to be local.

We’re going to do this as follows:

  1. Create a local gitignore file (call it e.g. .local.gitignore)

  2. Add it to the git config as shown above:

    git config --local core.excludesfile "<full-path-to-.local.gitignore>"
    
  3. Add a reference to the local gitignore file in the local gitignore file itself

    • i.e. the first line of .local.gitignore will be .local.gitignore (or even .local.* if you want to use this pattern for other files too)
    • this way, the file will ignore itself!

🚀 Terminal oneliners for quick setup

Run this in the root folder of your git repository.

Bash (Linux or Git Bash):

echo ".local.gitignore" > .local.gitignore && git config --local core.excludesfile "$(pwd)/.local.gitignore"

Powershell (Windows):

Set-Content .local.gitignore ".local.gitignore"; git config --local core.excludesfile "$PWD\.local.gitignore"

Tips

The contents of my local gitignore are mostly as follows:

.local.*
.local/

This ignores all files starting with .local. (such as .local.gitignore itself!), and also ignores a .local folder where you can keep your super secret files with whatever names you wish.