From time to time, you don't want git to track certain files. There are a few methods to tell git, which files to ignore.

exclude:

You can set a rule so that git doesn’t track specific files in the repo. For this, you need to put per-repo rules $DIR/.git/info/exclude. Edit exclude file and append the patterns, you want to ignore. But this option will only applied to your local repo. These rules are not committed with the repo so they are not shared with others.

Example:
# git ls-files --others --exclude-from=.git/info/exclude
# Lines that start with '#' are comments.
*.[oa]
*.pyc

With above changes, git will not track *.o, *.a and *.pyc files. Please notice that it supports regex matching of patterns.

This method can be used for locally-generated files that you don’t expect other users to generate, like files created by your editor.


.gitignore

If you want to share the rule list with any other users that clone the repo, you can create a file in your repo named .gitignore. git will use its rules when looking at files to commit. Please note that git will not ignore a file that was already tracked before a rule was added to this file to ignore it.

You can create .gitignore in any subpath to have its rules applied at that path.


Global .gitignore

If you are working on many repos, maintaining exclude files for each and every repo is hectic. Since we mostly have similar requirements for all the repos, you can create a global .gitignore file, which apply its rules to all the repos. You can create the file ~/.gitignore_global and add some rules to it. To add this to your config, run:

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

Example:

# Compiled source #
###################
*.com
*.class
*.dll
*.exe
*.o
*.so
*.lo
*.Plo
*.Po
*.la
*.pc
*Makefile

Please note that .gitignore_global file is per user, so it won't get tracked in version control. Meaning other developers would need to follow suite.


Subscribe - To get an automatic feed of all future posts subscribe here, or to receive them via email go here and enter your email address in the box. You can also like us on facebook and follow me on Twitter @akashag1001.