loading...

Git: Keeping Files from Being Tracked Without .gitignore

noelworden profile image Noel Worden ・2 min read

This Week I Learned (21 Part Series)

1) Beginning of a Blog Series 2) Accessing localhost within a Docker Image 3 ... 19 3) Identifying and Removing Hidden Characters 4) Getting Granular with Git Diff 5) Tweaking Logger Outputs on the Fly 6) Keeping Your Hands on the Keyboard: A Few Bash and Git Shortcuts 7) Breaking Down Elixir's `with` Expression 8) How To Write A Custom Elixir Schema Validation 9) The Handful of Commands I Use When Interactive Rebasing with Vim 10) Got 5 Minutes to Spare? Why Not Set up a Custom Commit Message Template? 11) Improving Your Commit Message with the 50/72 Rule 12) Simultaneously Looping Over Two Collections in Elixir 13) How to Utilize Enum.any?, with a Refactoring Twist! 14) A Rabbit Hole of Decimal Formatting 15) More Custom Validation Work: Manipulating a Keyword List To and From a Map 16) Stepped Away for a Long Weekend 17) So Many Ways to Update a Map with Elixir! 18) Git: Keeping Files from Being Tracked Without .gitignore 19) Git: Tracking Down a Committed File, with a Real-Time Ah-Ha Moment! 20) Setting up `.iex.exs` to Speed up Your Elixir Debugging (and How to Include It in a Docker Image) 21) Git: The Power of the Dash

This week I picked up a little gem around keeping files out of git.

I had made some tasks via VSCode and in doing so it created a .vscode file in the workspace. Git picked up this new file, so I instinctively went for .gitignore. I added .vscode with a small comment, committed, and pushed up a PR.

A colleague pointed out that .gitignore wasn't the best place for this file, because it was developer environment-specific and not project-specific. But he didn't stop there, he pointed me to another git dotfile that I didn't know existed!

Before I get into that dotfile, a little refresher on accessing dotfiles. If you're navigating to this file via the terminal, the basic ls command won't show dotfiles, it needs to be appended with -a, so the command is ls -a.

If you're on a Mac and navigating via the Finder, by default you won't see dotfiles either. Here's a one-liner you can run in terminal to show the dotfiles:

defaults write com.apple.finder AppleShowAllFiles YES; killall Finder /System/Library/CoreServices/Finder.app  

There are actually two actions happening in that command, the first is telling the OS to set AppleShowAllFiles to YES.

defaults write com.apple.finder AppleShowAllFiles YES

And the second is restarting the Finder app. When it completes the restart it will show all dotfiles, system-wide.

killall Finder /System/Library/CoreServices/Finder.app

Ok, now, back to the git stuff.

When a git repository has been created, with git init, it installs this .git folder, which contains everything git needs to track files, run hooks, etc. A little buried in that folder is a file titled exclude, found at this path:

.git > info > exclude

That exclude file basically acts as a .gitignore, but at a local level. So, in my case I just added .vscode, and, like git magic, it's ignored in my tracked files.


This post is part of an ongoing This Week I Learned series. I welcome any critique, feedback, or suggestions in the comments.

This Week I Learned (21 Part Series)

1) Beginning of a Blog Series 2) Accessing localhost within a Docker Image 3 ... 19 3) Identifying and Removing Hidden Characters 4) Getting Granular with Git Diff 5) Tweaking Logger Outputs on the Fly 6) Keeping Your Hands on the Keyboard: A Few Bash and Git Shortcuts 7) Breaking Down Elixir's `with` Expression 8) How To Write A Custom Elixir Schema Validation 9) The Handful of Commands I Use When Interactive Rebasing with Vim 10) Got 5 Minutes to Spare? Why Not Set up a Custom Commit Message Template? 11) Improving Your Commit Message with the 50/72 Rule 12) Simultaneously Looping Over Two Collections in Elixir 13) How to Utilize Enum.any?, with a Refactoring Twist! 14) A Rabbit Hole of Decimal Formatting 15) More Custom Validation Work: Manipulating a Keyword List To and From a Map 16) Stepped Away for a Long Weekend 17) So Many Ways to Update a Map with Elixir! 18) Git: Keeping Files from Being Tracked Without .gitignore 19) Git: Tracking Down a Committed File, with a Real-Time Ah-Ha Moment! 20) Setting up `.iex.exs` to Speed up Your Elixir Debugging (and How to Include It in a Docker Image) 21) Git: The Power of the Dash

Posted on by:

noelworden profile

Noel Worden

@noelworden

Software Engineer in Boulder, CO - Writing code and getting strategically lost in the mountains

Discussion

markdown guide
 

I would stick to .gitignore for excluding project files, precisely because .gitignore is committed and propagated. And you actually want to make sure everybody has them excluded, otherwise if someone has theirs committed and pushed, it'll mess up others' local setup when they pull it. Remember info/exclude is local per repo, meaning every time someone clones a repo, they need to remember to manually edit the file. That is very error-prone.