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

git update-index --assume-unchanged file

To undo and start tracking again:

git update-index --no-assume-unchanged [ ...]
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:

user@system: $ git pull
remote: Counting objects: 8, done.
remote: Compressing objects: 100% (8/8), done.
remote: Total 8 (delta 4), reused 0 (delta 0)
Unpacking objects: 100% (8/8), done.
From https://git.repo/project
   44db471..8d375ba  master     -> origin/master
Updating 44db471..8d375ba
error: Your local changes to the following files would be overwritten by merge:
Please commit your changes or stash them before you merge.

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

git checkout test.php
git pull

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:

git ls-files -v | grep '^[[:lower:]]'

Sample output

h database.config.php
h test.php

Useful links

GIT documentation
Magento 2 extensions on github

One thought on “GIT: Ignore changes in tracked file

  1. Thanks, exactly what I needed!
    My use case is that I have encrypted keys in a repo, and on my local systems these files are decrypted, but I have no plans to commit the unencrypted files any time in the near future 🙂
    Now I have peace of mind, as they don’t show up in the output of git status anymore.

Leave a Reply

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