How to create an awesome gitignore file

I was recently pushing some code for WordPress to a repository on BitBucket, and had an issue with total repo size. There is a 2GB limit for repositories on BitBucket, and unfortunately the site I was attempting to upload had a lot of images in the wp-content/uploads directory. In the process of fixing the issue, I learned a bit about .gitignore files. Not only is a .gitignore file great for restricting images from your repo (theoretically images don’t need to be version controlled), but it’s also good for restricting the upload of sensitive files such as those with DB connection credentials.

Creating a gitignore file is easy!

Navigate to the root of your Git repository.

Initialize and edit the file .gitignore with your favorite Linux text editor.

I prefer Nano.

# touch .gitignore
# nano .gitignore

creating a gitignore file

Find a .gitignore template to start with.

[emaillocker id=”21467″]

Head on over to the gitignore repo on GitHub. This repo has a collection of community maintained gitignore templates for creating ignore files related to some of the most common apps, languages, and frameworks (i.e. zend, perl, ruby, WordPress, etc).

Type whatever you’d like into your .gitignore file.

I added the following to mine:

*.log
.htaccess
sitemap.xml
sitemap.xml.gz
wp-content/advanced-cache.php
wp-content/backup-db/
wp-content/backups/
wp-content/blogs.dir/
wp-content/cache/
wp-content/upgrade/
wp-content/uploads/
wp-content/wp-cache-config.php
wp-admin/
wp-includes/
wp-*.php
!wp-config.php
index.php

Basically, this file says ignore the .htaccess and sitemap files, some heavy image directories within the wp-content folder, and most of the core files in the root except for wp-config.php.

[/emaillocker]

Save the .gitignore file, commit it to your repo, and away you go. For any future commits, no new files restricted within your .gitignore file will be added.

Questions? @ifyouwillit

More resources:
Git ignore
When git ignores your… .gitignore?

5 thoughts on “How to create an awesome gitignore file”

  1. If you added a bunch of images in one commit, and follow it up with this commit that adds the .gitignore file, the files that are bulking up your git history and .git folder are still there. After adding the .gitignore file, it’s a good idea to rebase your git history or use some other method to remove the images from your previous commits.

    1. Thanks for adding that. This is not retro-active, so it will not automatically remove files that have already been uploaded to the repository but excluded. It’s best to implement .gitignore during the initial commit.

  2. Thanks, very useful post… Can I just add that if you happen to have already added .htaccess into git then want to add it to .gitignore that’s fine but you must first stop tracking it with ‘git rm .htaccess’. Took me a little while to figure this one out!

Leave a Comment

Your email address will not be published. Required fields are marked *

Posted in