Hide files from git, but everywhere!

Git has a concept of global gitignore. You can have your usual files in a repository without needing to add them to the project's .gitignore file. Here's why and how.

Hide files from git, but everywhere!

I work on a bunch of sites. I did some housekeeping earlier and removed ~30 folders for sites I no longer want to deal with.

One of the most important things I do on sites is trying to figure out what the hell went wrong in the first place. In order to help me do that, I have a bunch of tools that I use on pretty much every site.

Laravel DD plugin. This one: https://wordpress.org/plugins/laravel-dd/. It allows me to see precisely what the contents of the variables are with a syntax that is literally this:

dd( $var, $another_var, $third_var, $_SERVER, __FILE__ . ':' . __LINE__ );

Comes from Laravel land: https://laravel.com/docs/5.7/helpers#method-dd. Useful.

Another is a plugin I just put in the wp-content/plugins/a.php file. It’s literally just a plugin header, and then whatever I need to test.

Need to test whether an action fires?

add_action( 'whatever_action', function() {
    dd( 'stop here', __FILE__ . ':' . __LINE__ );
}

You get the gist.

Query monitor, debug bar, logging plugins are also among the tools I use.

However, all of the projects I’m working on are version controlled. If I have files / folders that are untracked, git will complain that the state of the repo is dirty, because of these untracked files / directories:

$ git status
On branch fix/wc3-compatibility
Untracked files:
  (use "git add <file>..." to include in what will be committed)

	wp-content/plugins/laravel-dd/

nothing added to commit but untracked files present (use "git add" to track)

I could add those to the project’s .gitignore file, however then I have a dirty repository because I changed a file that IS tracked. Plus do I really want to let the project owners know what tools I used to debug issues? Even if I remove them later from the file, it still feels unnecessary.

So it seems either way we’re losing because we need to change the contents of the repository.

Or do we? ...

What if we could hide files from a project, from outside of the project?

So git is pretty smart. It has a concept called “global gitignore”. https://help.github.com/articles/ignoring-files/#create-a-global-gitignore

You can also create a global .gitignore file, which is a list of rules for ignoring files in every Git repository on your computer.
$ touch ~/.gitignore_global
$ git config --global core.excludesfile ~/.gitignore_global

Then all you need to do is open the file with your editor of choice, and add some rules to it. I have the following:

.vscode/
laravel-dd/
wp-content/plugins/a.php

And tadaa, all of those rules will form part of the project’s own .gitignore rules. Automatically. Without you having to add them one by one.

Photo by Oscar Keys on Unsplash