Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

As a dev, do you use .gitignore to ignore everything and purposefully include? Or do you just exclude with it?

Tags:

gitignore

We have an internal discussion going here and we are somewhat torn on the best practice for using .gitignore on projects that contain a lot of files (like a CMS).

Method 1

Method 1 would be to purposefully .gitignore all files that come standard with your build. That would generally start like:

# ignore everything in the root except the "wp-content" directory.
!wp-content/

# ignore everything in the "wp-content" directory, except:
# "mu-plugins", "plugins", "themes" directory
wp-content/*
!wp-content/mu-plugins/
!wp-content/plugins/
!wp-content/themes/

# ignore these plugins
wp-content/plugins/hello.php

# ignore specific themes
wp-content/themes/twenty*/

# ignore node dependency directories
node_modules/

# ignore log files and databases
*.log
*.sql
*.sqlite

Some staff members like this approach since if you create something outside of the standard files, for example like a /build folder, then it would automatically be detected for inclusion. However, writing custom theming and plugins require you to add a few layers to this file to "step in" to the folders you want to keep, and generally, the file is a bit messier to read.

Method 2

Method 2 ignores everything, and then you whitelist what you want in the repo. That would look like

# Ignore everything, but all to descend into subdirectories
*
!*/

# root files
!/.gitignore
!/.htaccess.live
!/favicon.ico
!/robots.txt

# theme
!/wp-content/themes/mytheme/**
/wp-content/themes/mytheme/style.css # Ignore Compiled CSS
/wp-content/themes/mytheme/js # Ignore Compiled JS

# plugins
!/wp-content/plugins/my-plugin/**

# deployment resources
!/build/**

Some staff like this since it's cleaner, you have to purposefully add something (which makes accidental adds harder), and it also in effect shows you your .git folder structure.

What is the best practice? Which method do you enjoy and would you recommend doing one over the other?

like image 818
hdwebpros Avatar asked Aug 01 '19 20:08

hdwebpros


People also ask

Should I include Gitignore in Gitignore?

gitignore file's purpose is to prevent everyone who collaborates on a project from accidentally commiting some common files in a project, such as generated cache files. Therefore you should not ignore . gitignore , since it's supposed to be included in the repository.

When should I use Gitignore?

When Do You Use Git Ignore File? The git ignore file rule allows you to ignore a file you've committed in the past. You use it when you do not want to recommit a file, for example a build artifact.

What would you use a .gitignore file for?

gitignore file tells Git which files to ignore when committing your project to the GitHub repository. gitignore is located in the root directory of your repo. / will ignore directories with the name.

How do I Gitignore everything?

You want to use /* instead of * or */ in most cases The above code would ignore all files except for . gitignore , README.md , folder/a/file. txt , folder/a/b1/ and folder/a/b2/ and everything contained in those last two folders.


1 Answers

The second method is the best practice, when it comes to exlude some folder contents of gitignore rules.

It better reflect the following rule:

It is not possible to re-include a file if a parent directory of that file is excluded.

To exclude files (or all files) from a subfolder of an ignored folder f, you would do:

f/**
!f/**/
!f/a/sub/folder/someFile.txt

Meaning: you need to whitelist folders first, before being able to exclude from gitignore files.

It is clearer, shorter (unless you have a large number of folder to whitelist)

What if it is a Joomla install with a large amount of directories and files?
Or what if a core upgrade adds new files or folders

Don't forget you can have multiple gitignore files, one per folder.
That means you can mix and match both approaches.

And you have:

  • http://gitignore.io/ (which does blacklist when it comes to Joomla application)
  • github/gitignore (same approach for Joomla)
like image 132
VonC Avatar answered Oct 19 '22 11:10

VonC