Skip to main content

No More Adding *.swp in .gitignore

Every time I create a new git repo, I end up needing to create a new .gitignore file. Vim temp files keep showing up, begging to be put into the repository. It's like the gnats when you mow the lawn, you can ignore them for awhile, but then you just have to do something about it.

While working on the post "Better Git Commit Messages with Vim", I reviewed git customization 1 to ensure I had 'core.template' and 'core.editor' information correct. I ran across an option I don't remember seeing before, 'core.excludesfiles' 2. They address those very gnats as a super potent bug spray.

You can put patterns in your project’s .gitignore file to have Git not see them as untracked files or try to stage them when you run git add on them, as discussed in Ignoring Files.

But sometimes you want to ignore certain files for all repositories that you work with. If your computer is running Mac OS X, you’re probably familiar with .DS_Store files. If your preferred editor is Emacs or Vim, you know about filenames that end with a ~ or .swp.

What! How could I have missed this for so long? Now, we can ignore not only vim recovery files, but those ever present Python cache files, __pycache__.

First, create a text file fille with out favorite, chronic problem files and name it ~/.gitignore. Now, add this to our global git config.with

$ git config --global core.excludesfile ~/.gitignore

and those pesky files won't bother me again. This doesn't mean we won't need .gitignore files anymore. It just means during those first, exciting steps in repository creation, we will not be swatting away files. It also means we have a great jumping off point when I do need to create a .gitignore file. A simple

$ cp ~/.gitignore .

and I am ready to get repo specific. Here is mine if you are interested.

.gitignore (Source)

*.swp
__pycache__/
*.py[cod]

Footnotes

1

Customizing Git, Git Configuration

2

Customizing Git, core.excludesfiles