What is gitignore

Gitignore is a special file that tells Git what files you want to automatically exclude from repositories. There are certain types of files that you don't want to commit because they're not actually part of your project, or because they are impractical to include in version control.

You don't want version control to waste space saving copies of already compiled materials, especially those that quickly go out of date. If you're never going to use them again, they're a poor fit for version control management.

As for assets, most data files won't change during development. Using version control to track memory intensive art, sounds, databases and other data files isn't a productive use of this tool.

Global gitignore vs repository gitignore

A .gitignore file normally lives at the top level of your repository, it specifies which files you want to automatically exclude from your commits. Alternatively, you can use Git to build a global .gitignore file in your home directory, that will excludes files for all repositories.

Why global

If you create global .gitignore file properly, you can basically set it and forget it. You get all the advantages of .gitignore in every Git project you use. It's way easier than using separate .gitignore files for each and every project.

Since other people using the repositories won't necessarily be using the same setup, they wouldn't benefit from having your specific ignores in the repo's gitignore.

OS specific files

Operating systems can leave a lot of junk, both visible and invisible, lying around your computer. These files are usually generated as a part of normal system operations. For example, you might find a Windows thumbnail cache, or a folder customization file that remembers icon positions or stores a background image. Since these files are used for display purposes by your computer, they don’t affect your project in any way, and you want to omit these OS specific items.

# OS generated files #
######################
.DS_Store
.DS_Store?
.AppleDouble
.LSOverride
Icon
._*
.Spotlight-V100
.Trashes
ehthumbs.db
Thumbs.db
[Dd]esktop.ini
$RECYCLE.BIN/

IDE / Code editor specific files

Application files also are not required by your project at all. It is also unlikely that any of your team members would require these files in order for them to work on their project. Hence, we ignore application files.

# Logs and databases #
######################
*.log
*.sql
*.sqlite

### Node.js logs ###
npm-debug.log*
yarn-debug.log*
yarn-error.log*

### Runtime data ###
pids
*.pid
*.seed
*.pid.lock

### Dependencies ###
node_modules/
jspm_packages/
bower_components/
vendor/

### IDEs and code editors ###
.idea/
.idea_modules/
.vscode/*
.vs/
nbproject/
atlassian-ide-plugin.xml
.sass-cache/
build/*

### Vim ###
# Swap
[._]*.s[a-v][a-z]
[._]*.sw[a-p]
[._]s[a-rt-v][a-z]
[._]ss[a-gi-z]
[._]sw[a-p]
Find all necessary excludes

You can use Gitignore.io service to build .gitignore files. It generates a .gitignore file for you using all kinds of keywords you searched. Also you could find plain .gitignore files in this GitHub repository.

gitignore.io web tool

How to do it

You tell Git which file to use by issuing a global Git config command, and here it is. This starts with git config --global which configures Git globally across your personal computer account. It applies to any repo your account creates. The name of the option you're setting is core.excludesfile. And the path of the file you're setting this to is a .gitignore --global file in your home directory which is what that tilde at the start of this path means.

This command does not create this new file for you, it just configures Git to know where to look for the file, where that file is supposed to be.

Let's create a new empty global .gitignore file in your home directory:

$ cd ~
$ touch .gitignore_global

Open this file and fill it out with a list of rules for ignoring files in every Git repository on your computer. As a final step, let Git knows, that you want to use this file as a global .gitignore by adding it to your Git config with the following command:

$ git config --global core.excludesfile ~/.gitignore_global

Clean up commited files

Cleaning up commited files that should be ignored

If you already have a file checked in, and you want to ignore it, Git will not ignore the file if you add a rule later. In those cases, you must untrack the file first, by running the following command in your terminal:

$ git rm --cached {FILENAME}

Committing an ignored file

It is possible to force an ignored file to be committed to the repository using the -f (or --force) option with:

$ git add -f debug.log

You might consider doing this if you have a general pattern (like *.log) defined, but you want to commit a specific file. However a better solution is to define an exception to the general rule:

$ echo !debug.log >> .gitignore
$ cat .gitignore
*.log
!debug.log
$ git add debug.log

This approach is more obvious, and less confusing, for your teammates.