How to Ignore Files Already Tracked in Git
If you need to ignore a file that is already tracked in Git, normal .gitignore
will not work. There are ways to do this using Git’s special commands: assign-unchanged and
skip-worktree
. This article details how to use each command and the differences between them.
1. The Need to Ignore Files
Depending on your development environment and project, there may be files that you do not want changes to be reflected in the repository, such as configuration files or log files. If these files are accidentally committed, unintended changes may affect other developers.
2. How to use ASSUME-UNCHANGED and SKIP-worktree
Git has two commands ,
assume-unchanged and
skip-worktree, that
temporarily ignore changes to files. They look similar at first glance, but there are slight differences in usage and behavior.
ASSUME-UNCHANGED
Assume-unchanged
is a command that prevents Git from being notified when a file change occurs. With this command, local file changes are no longer included in staging or commits.
# Command to use assume-unchanged
$ git update-index --assume-unchanged path/to/file.txt
To revert back, use the following command.
# The command to revert to tracking changes
$ git update-index --no-assume-unchanged path/to/file.txt
skip-worktree
skip-worktree
differs from normal file ignore in that it causes Git to ignore changes to the file. This command makes Git assume that the file is “local only” so that Git is unaware of its changes.
# Command to use skip-worktree
$ git update-index --skip-worktree path/to/file.txt
To revert, use the following command.
# Command to revert to tracking changes
$ git update-index --no-skip-worktree path/to/file.txt
3. Differences and usage
assume-unchanged: Ignores the fact that there are changes for performance purposes, but does not affect the file state of the repository. This is useful for reducing build time in large repositories.
skip-worktree: Used to ignore certain files during team development. Usually used when configuration files and other files differ in the local environment.
4. Notes.
These commands are temporary and do not completely remove the tracking status of a file. Also note that they do not apply to other developers, so files are not completely ignored.
Conclusion
When ignoring a file that is already managed by Git, you can prevent changes to the file from being reflected in the repository by properly using assign-unchanged and
skip-worktree
. Use them according to your project situation to prevent unintended commits.