Blog

04
December 2013

Gavin Pickin

Git for Dummies - Using GITIGNORE files to exclude certain files and folders

Source Control

After a little break, I am back with my Git for Dummies series. We've covered a lot so far, and we're starting to use Git now, and as we continue in this series, we'll start to see a few more day to day tasks, and we'll introduce some of the bigger concepts as we face a few hurdles. Thankfully, Git makes these hurdles into more of a speed bump, one of them that I was happy to get working, was GitIgnore, to exclude files and folders that I don't care about, or don't want polluting my repos.

So far we have covered Why you should use Source Control, Not Using Source Control, amazingly you're not aloneInstalling Git on WindowsInstalling Git on Mac OSXInstalling Git on LinuxYour First RepoSetting up your first Remote Repo on BitbucketCreating your SSH Keys for BitbucketHow to set your Default Name and Email in Git, Converting a Project into a Git RepoPushing Code from Dev to Production with Git, and today... using GitIgnore to exclude and certain files and folders.

A lot of the time, you have a project, and inside the folder that contains your repo, you have files and folders for errors, debugging information, temp files, and are not important to your repo, and source control on these files is not needed, and in some cases, with big files, source control can be downright time consuming and bandwidth consuming. Git allows you to configure your repos on many levels, global settings, repo settings, and then per folder. Like all of your other settings in Git, Git stores these settings in hidden files, in the .git directory, or in your repo in a directory by directory basis. Git gives you the tool to help manage you repo... with .gitignore files. 

For settings for your entire repo, you can create a .gitignore file in the root of your repo... or if you want your settings to only affect a single directory, you can place a .gitignore in the directory itself. Git Ignore files are pretty simple... you list the files, or folders you want to ignore, with or without wildcards, and git will ignore those files, and you can list the files or folders you don't want to ignore... confused, lets see a couple of examples.

If you want to ignore smtp.log and mail.log you would have a .gitignore like this

smtp.log
mail.log

If you want to ignore all files in a directory, you can just reference the directory itself, with Railo and Coldfusion, you will want to usually ignore the WEB-INF folder, so lets ignore that folder

WEB-INF

Now, what if we want to have the WEB-INF folder, but not any of the files?
We can put a .gitignore file in the WEB-INF folder, and we can ignore all files, using the * asterisk... but maybe, we want to leave a readme.txt file in there for notes on the WEB-INF folder... but ignore the rest... it would look like this.

#ignore all files
*

#but not
readme.txt

Note, we put a couple of comments. Like in most programming, if the reason for doing something is not obvious, please leave comments, whether its for you, or someone else later, a comment will take a second now, and save minutes and hours later.

Now, in this case, we don't want the WEB-INF folder at all, so we're going to put a .gitignore file in the root, and it looks like this (I like to break down the directories and files, so its easier to digest.

#Ignore these directories
WEB-INF

#ignore these files

Great we're done... BUT, if you have the folder WEB-INF and all the files already added and committed to your repo, those files will remain in the file, until you clean them out of the cache. To clean the cache, we can use the following command.

$ git rm -r --cached WEB-INF

This removed the entire folder from the cache, and then we just need to commit the changes and push to your remote repo.

$ git commit -m "Ignoring and removing WEB-INF from repo"
$ git push

Now, when you check your remote repo, you'll see those files are no longer in your repo.
If you have a lot of files, not just one directory like WEB-INF, you can use a wildcard to remove all files, and then add all the files back, and then commit the updates

$ git commit -m "Pre Cleanup"
$ git rm -r --cached .
$ git add .
$ git commit -m ".gitignore setup and cleaned up"

As with everything... please practice with these commands before using them with your projects... to make sure you know what you're doing, before you do anything with production or staging servers.

Let me know if any of this is not accurate, or needs clarifying, as I said before, I'm not an expert, but these are lessons I have learned on my way to being a better user of Source Control.

Thanks for reading along, let me know if you have any questions, or ideas for future articles, we still have to cover branches, learning how to checkout code to fix an error, and how to roll back to a previous commit.

Thanks again. Happy Gitting

Gavin

by Jim Priest
12/04/2013 07:22:03 PM

Great post. From what I understand you can also have a 'global' ignore file which impacts any git project you interact with.

For example I use Sublime and don't necessarily want my Sublime project files going in the repo. So instead of adding that to each and every project I add it to my global ignore file and don't have to worry about it again.

by Peter Boughton
12/05/2013 03:49:54 AM

Having .gitignore files everywhere is ugly.

As Jim says, put globally ignored/excluded files (like .project files) in the global location (located with "git config --global core.excludesfile"), then any project-specific ones go to "{project}/.git/info/exclude" file and it's all out of the way.

by Gavin
12/05/2013 08:30:22 AM

You guys are absolutely right. Thanks for the feedback.

I was planning on doing a follow up post on global settings, since that is great for all projects, like .DS_Store file on Mac OSX, you can globally ignore them etc.

One thing to remember about the .git/info/exclude file, it is not committed and shared, so it is only good for your local repo. One of the good things about the scattered .gitignore files is that they are committed (unless you ignore them too) and they can be shared, which is useful in some situations.

I also intend to give more examples of items you might want to ignore, and give links to files you may want to ignore globally, Github has a guide https://help.github.com/articles/ignoring-files and even has a repo for a list of .gitignore files based on the OS and projects you might be working on found here https://github.com/github/gitignore I decided putting the comments in the post would be useful for those who read this post, but I'll also do a more complete post shortly for those wanting to delve a little deeper.

Thanks for reading, and the feedback... much appreciated by me, and the readers here too.

Blog Search