git
git

.gitignore

Create a file, and name it as .gitignore in the root directory, then add names and patterns for any files and directories that should not be added to the repository.

Use asterisk (“*”) as a wildcard. E.g. “*.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 with other team members. 

Git hosting vendors like GitHub provides standard .gitignore templates for popular languages.

.git/info/exclude

.gitignore should be committed to the repository, which means all team members will share the same set of ignored files. There are times, we just want to ignore files locally, but not applicable to other team members. This can be done bycould 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

skip-worktree

.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.

assume-unchanged

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.

Comparison Table

METHODDESCRIPTIONUSE CASESSCOPE
.gitignore filePrevents files from being added to the repository.Local settings, compiler output, test results, etc.Global
.git/info/exclude filePrevents local files from being added to the repository.Local settings, compiler output, test results, etc.Local
skip-worktree settingPrevents local changes from being committed to an existing file.Shared files that will have local overwrites, like config files.Local
assume-unchanged settingAllows Git to skip files that won’t be changed for performance optimization.Files and folders that a developer won’t touch.Local

Last modified: November 25, 2020

Author