Git Tips & Tricks

GIT: Ignore changes in tracked file

I had some experience with GIT, but it was limited to basic things like pull, commit, push. Recently I started working on project which has database configuration file. I needed to modify it, but do not want to commit any changes made. Basically I wanted git to ignore changes in tracked file.

My first attempt was to add it to .gitignore, but it does not work. According to gitignore documentation files already tracked by Git are not affected.

How to ignore changes in tracked file

The solution is to use git-update-index

To undo and start tracking again:

When this flag is specified, the object names recorded for the paths are not updated. Instead, this option sets/unsets the “assume unchanged” bit for the paths. When the “assume unchanged” bit is on, the user promises not to change the file and allows Git to assume that the working tree file matches what is recorded in the index. If you want to change the working tree file, you need to unset the bit to tell Git. This is sometimes helpful when working with a big project on a filesystem that has very slow lstat(2) system call (e.g. cifs).

Git will fail (gracefully) in case it needs to modify this file in the index e.g. when merging in a commit; thus, in case the assumed-untracked file is changed upstream, you will need to handle the situation manually.

Will git ignore untracked files if I pull in modifications to them?

If there are any local changes to file(s) marked as assume-unchanged, you will get an error like this:

What you can do is revert your local changes and pull it once again. After that apply changes again.

List of ignored files

if you forgot what files were untracked you can use git ls-files -v. If the “h” character printed is lower-case, the file is marked assume-unchanged. To print just the files that are unchanged use:

Sample output

Useful links

GIT documentation
Magento 2 extensions on github

Leave a Reply

Your email address will not be published. Required fields are marked *