Have you ever run into an issue where you have files in your Git repository that should not be there? Examples could be full databases, log files, compiled binaries, vagrant directories, virtual machines, jar files, OS generated files like thumbnails, archives, and so on. This is going to be a short how-to for removing those files from your repository (but they will still stay intact on your local system).

You can create a configuration file that is used to ignore these files. This can be done at the repository level or the global level.

Gitignore File Contents

The contents of the file are structured like so:

# The character '#' is used to denote comments

name-of-file.extension

Wildcards also work:

# Using wildcards

*.extension

*.zip

*.sql

Using wildcards like the ones above allow you to exclude every file of a particular type, based on file extension. If you do not want all .zip files included, it would be *.zip. You can find common git ignore files online via google and other resources. Here is one that GitHub provides.

This is an example of my current gitignore file that I am using at the time of writing this post:

# Compiled source #

###################

*.com

*.class

*.dll

*.exe

*.o

*.so # Packages #

############

# it’s better to unpack these files and commit the raw source

# git has its own built in compression methods

*.7z

*.dmg

*.gz

*.iso

*.jar

*.rar

*.tar

*.zip # Logs and databases #

######################

*.log

*.sql

*.sqlite # OS generated files #

######################

.DS_Store

.DS_Store?

._*

.Spotlight-V100

.Trashes

ehthumbs.db

Thumbs.db # Vagrant stuff

.vagrant

Local vs. Global

You can place a gitignore file into the root directory of your repository. It should have the following name:

.gitignore

Git will ignore files that match the rules within the file by default. This is useful for a variety of reasons, some of which may be the following:

You know those rules only apply to this specific project. You want the rules to be common with anyone that contributes to your project, as the .gitignore file will be checked into the repository.

If you wish to make a ruleset “global”meaning it applies to every repository you ever create on your system, you can create a new gitignore file, place it in your home directory (or somewhere else that makes sense on your system), and run the following command:

git config --global core.excludesfile ~/.gitignore

In this case, the git ignore file is located within ~/.gitignore, but the file could really be named anything and stored anywhere accessible by git. It is a common practice to store it in your home directory (~) as a hidden file. Note that this file will not be included with your repository, so your ruleset will not be shared with your team.

Removing Files from the Repository

Now that you have your gitignore files in place, you may have noticed the ignored files are still in your repository. Please note that setting up this config file does not effect files already pushed to the repository — those will have to be removed.

Commit any outstanding code changes. Do not skip this step — if you proceed before doing this step, you will lose those changes.

Then, you will want to remove any changed files from the index(staging area) by running this command:

git rm -r --cached .

Now you can re-add your files (which will now take the gitignore file into consideration):

git add .

git commit -m "Removed gitignore files from repository"

I ran into this issue today and figured I would put all this information in one place for my future self — and who knows, it could be useful to others. Here are some resources I used when researching the content for this post (some have already been linked to):

github/gitignore: A collection of useful .gitignore templates

https://github.com/github/gitignore

version control — Ignore files that have already been committed to a Git repository — Stack Overflow

https://stackoverflow.com/questions/1139762/ignore-files-that-have-already-been-committed-to-a-git-repository

Ignoring files — User Documentation

https://help.github.com/articles/ignoring-files/