In Git, you can temporarily ignore changes to a tracked file without modifying the .gitignore
file by using the --assume-unchanged
flag. This is useful for avoiding accidental commits of local changes to files that are already tracked by Git, such as configuration files or environment-specific settings. It’s particularly helpful for local-only modifications that shouldn’t affect the project history.
Key Commands
- Temporarily Ignore Changes to a File:
git update-index --assume-unchanged <file>
- Re-enable Tracking for a File:
git update-index --no-assume-unchanged <file>
- List All “Assumed Unchanged” Files:
git ls-files -v | grep '^[a-z]'
See the section “Explaining the Command for List All” below for an explanation.
Important Notes
- The
--assume-unchanged
flag is intended for performance optimization and temporary use. It does not prevent Git from overwriting the file during operations likecheckout
ormerge
. - For long-term exclusions, consider updating
.gitignore
or using.git/info/exclude
instead.
Explaining the Command for “List All”
git ls-files -v
lists all the files in Git index (Staging area) line by line. Each file is prefixed by a letter (Eg:H
,M
,R
,h
,m
,r
.). These are called “Status Flags”. Upper case letters refer to normal (Not assumed unchanged) files. Lowercase letters refer to “assumed unchanged” files.|
Pipes the output ofgit ls-files -v
intogrep
.grep '^[a-z]'
filters the output ofgit ls-files -v
to show only lines where the first character is a lowercase letter.
Meaning of Status Flags
- H: tracked file that is not either unmerged or skip-worktree.
- S: tracked file that is skip-worktree.
- M: tracked file that is unmerged
- R: tracked file with unstaged removal/deletion
- C: tracked file with unstaged modification/change
- K: untracked paths which are part of file/directory conflicts which prevent checking out tracked files
- ?: untracked file
- U: file with resolve-undo information
Source: https://git-scm.com/docs/git-ls-files#Documentation/git-ls-files.txt–t