public
Last active

Template .gitignore file for WordPress projects

  • Download Gist
.gitignore
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
# This is a template .gitignore file for git-managed WordPress projects.
#
# Fact: you don't want WordPress core files, or your server-specific
# configuration files etc., in your project's repository. You just don't.
#
# Solution: stick this file up your repository root (which it assumes is
# also the WordPress root directory) and add exceptions for any plugins,
# themes, and other directories that should be under version control.
#
# See the comments below for more info on how to add exceptions for your
# content. Or see git's documentation for more info on .gitignore files:
# http://kernel.org/pub/software/scm/git/docs/gitignore.html
 
# Ignore everything in the root except the "wp-content" directory.
/*
!.gitignore
!wp-content/
 
# Ignore everything in the "wp-content" directory, except the "plugins"
# and "themes" directories.
wp-content/*
!wp-content/plugins/
!wp-content/themes/
 
# Ignore everything in the "plugins" directory, except the plugins you
# specify (see the commented-out examples for hints on how to do this.)
wp-content/plugins/*
# !wp-content/plugins/my-single-file-plugin.php
# !wp-content/plugins/my-directory-plugin/
 
# Ignore everything in the "themes" directory, except the themes you
# specify (see the commented-out example for a hint on how to do this.)
wp-content/themes/*
# !wp-content/themes/my-theme/

The "!" exclusion feature doesn't work with git 1.5.4.1. Does with git 1.7.1.

This is awesome, thank you!

By using ! to invert the exclusion includes all stuff from the path
Since I have a .DS_Store file in my theme directory, it get's included in the repo, because of the inversion.
Is there a solution?
Example (last two lines from the gist above):

wp-content/themes/*
!wp-content/themes/my-theme/

Now if I have a .DS_Store file in wp-content/themes/my-theme/ it will be included in the repo.

@avioli perhaps put your .DS_Store line after everything else?

@avioli you may want to check that you have .DS_Store specified in your project's .gitignore file or your global .gitignore list. If you're depending on global gitignore's, it could also be that someone else who's working on your project doesn't have .DS_Store in their global .gitignore and subsequently added the .DS_Store file to the project.

Either way, the negation that re-includes your specific theme directory shouldn't override .DS_Store's exclusion.

Now that the file's been added, you'll need to manually remove it from the repository for the .gitignore to take effect again.

@avioli re-reading your question, it sounds as though you may be posing a hypothetical problem rather than describing one you've encountered, so I'd like to follow up with a little more explanation.

The negation that matches the .../my-theme/ path should not match .DS_Store files; even if .DS_Store is a pattern in a global .gitignore file or a pattern that appears earlier in the same file, it will remain excluded after the .../my-theme/ negation.

The reason for this is that the negation is applied to the path:

!wp-content/themes/my-theme/

This line tells git to remove the .../my-theme/ path from its list of exclusions, but the pattern doesn't use any wildcards that might apply to individual files within that path, so it won't override the earlier .DS_Store exclusion. To override file exclusions such as .DS_Store, we'd need to follow it up with:

!wp-content/themes/my-theme/*

...which would apply to all files within the .../my-theme/ path, and would therefore override the .DS_Store exclusion pattern. (So don't do it!)

Hope that helps. .gitignore patterns can be a little confusing.

(An aside: ending patterns with a slash to specify paths is relatively recent; as @benklocek pointed out, earlier versions of git don't support this.)

@jbartlett Thank you for the clarification. I didn't have .DS_Store inside .gitignore. I thought all dot-named files/folders are excluded from the git repo by default.

Great gist !

I use git submodule (for plugins) and in that case you must add:

!.gitmodules

I find whenever I'm changing branches using this gitignore, my plugins disappear when I merge back into the master, has anyone else encountered this problem?

Posted a question on stackoverflow to no avail

Hmmm... I can't seem to make the /* work -- it insists on grabbing everything. (I'm a git newbie so it's likely that I'm missing something...)

OK. I think I got it now. I needed to create my .gitignore file PRIOR to adding all the files using git add . . Hopefully this'll help some other newbie out there someday...

This is still really useful 2 years later!

Just wanted to say a simple thank you!

Great!, Now I can use the same directory for the clone and the rest of the code without using symlinks

Thank you for this setup man, I forked it to a version that I am using for my latest project. Cheers.

Fantastic, thanks so much :)

How I exclude the README.md file.
Everything is fine whis this gists except that the README.md file isn't displaying
Thanks

Thank you for this contribution, saved me a lot of time :)

I am trying to implement this on a Wordpress to limit version control on plugin and theme subdirectories. How to clone the full site though to get started since most of the site is ignored? What order and steps are to be taken? I first added all files except the custom plugin and theme subdirectories but when i clone, I get an error that the directory is not empty.

I have experienced an issue where untracked files do not show in my working tree. This was caused by the trailing slashes after directories. Example:

/*
!wp-content/
wp-content/*
!wp-content/themes/
wp-content/themes/*
!wp-content/themes/theme/

Should be..

/*
!wp-content
wp-content/*
!wp-content/themes
wp-content/themes/*
!wp-content/themes/theme

Great example.
I have added also wp-config.php.
I am using git versioing for wp for getting updates from dev, to tst and then from tst to production, on each of these wp-config.php is different. What is your opinion?

Update: I suppose this .gitignore excludes everything in the root so it means also wp-config:-)

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.