Git is one of the most popular version control systems (VCS) available, especially thanks to hosting vendors like GitHub. It keeps code safe and shareable. Sometimes, however, certain files should not be shared, like local settings or temporary configs. Git provides a few ways to make sure those files are ignored.
The easiest and most common way to ignore files is to use a gitignore file. Simply create a file named .gitignore in the repository’s root directory. Then, add names and patterns for any files and directories that should not be added to the repository. Use the asterisk (“*”) as a wildcard. For example, “*.class” will ignore all files that have the “.class” extension. Remember to add the .gitignore file to the repository so that it can be shared. As a bonus, Git hosting vendors like GitHub usually provide standard .gitignore templates for popular languages.
Any files covered by the .gitignore file will not be added to the repository. This approach is ideal for local IDE settings like .idea or .vscode, compiler output files like *.class or *.pyc, and test reports. For example, here’s GitHub’s .gitignore template for Java:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
|# Compiled class file|
|# Log file|
|# BlueJ files|
|# Mobile Tools for Java (J2ME)|
|# Package Files #|
|# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml|
As a best practice, .gitignore should be committed to the repository, which means all team members will share the same set of ignored files. However, some files should be ignored locally and not globally. Those files could be added to .gitignore, but large .gitignore files become cryptic and more likely to break other people’s setup. Thankfully, git provides a local-only solution: the .git/info/exclude file (under the repository’s hidden .git directory). Simply open it with a text editor and add new entries using the same file pattern format as .gitignore.
# Append a new file to ignore locally echo "my_private_file" >> .git/info/exclude
A .gitignore file prevents a file from being added to a repository, but what about preventing changes from being committed to an existing file? For example, developers may want to safely override settings in a shared config file for local testing. That’s where skip-worktree comes in: it allows a developer to “skip” any local changes made to a given file. Changes will not appear under “git status” and thus will not be committed.
Use the following commands:
# Ignore local changes to an existing file git update-index --skip-worktree path/to/file # Stop ignoring local changes git update-index --no-skip-worktree path/to/file
Warning: The skip-worktree setting applies only to the local repository. It is not applied globally! Each developer will need to run the skip-worktree command in their local repository.
Another option for ignoring files is assume-unchanged. Like skip-worktree, it makes Git ignore changes to files. However, whereas skip-worktree assumes that the user intends to change the file, assume-unchanged assumes that the user will not change the file. The intention is different. Large projects using slow file systems may gain significant performance optimizations by marking unused directories as assume-unchanged. This option also works with other update-index options like really-refresh.
Use the following commands:
# Assume a file will be unchanged git update-index --assume-unchanged path/to/file # Undo that assumption git update-index --no-assume-unchanged path/to/file
Again, this setting applies only to the local repository – it is not applied globally.
Which is the best way to ignore files?
|Method||Description||Best Use Cases||Scope|
|.gitignore file||Prevents files from being added to the repository.||Local settings, compiler output, test results, etc.||Global|
|.git/info/exclude file||Prevents local files from being added to the repository.||Local settings, compiler output, test results, etc.||Local|
|skip-worktree setting||Prevents local changes from being committed to an existing file.||Shared files that will have local overwrites, like config files.||Local|
|assume-unchanged setting||Allows Git to skip files that won’t be changed for performance optimization.||Files and folders that a developer won’t touch.||Local|
- Git project page
- Git Wikipedia article
- gitignore doc
- gitignore templates from GitHub for popular languages
- git update-index doc
- Git skip-worktree and how I used to hate config files
- Ignore specific files only on current machine in Git
- Exclude files from git without committing changes to .gitignore