Gabriel
Gabriel

Reputation: 42469

git ignore tracked file but keep in repo (assume-unchanged does not work)

I'm trying to ignore all changes to a very small file in my repo (Github). It is a hidden file (.first_run), which contains a single character (0). I use it (obviously) to detect a first run of my package.

I'm encountering these issues:

  1. The file is already tracked, so adding it to my .gitignore won't work.
  2. I can't use git rm --cached <file> because it will remove that file from the repo, which is not what I want.
  3. Using git update-index --assume-unchanged <file> also does not work. If, after applying assume-unchanged to the file, I change the 0 for a longer string (e.g.: sdfgdgd), it does not show as changed (correct behaviour) But if I change the 0 for say a 4, it will show up as modified.

I assume the issue described in point 3. is due to what is explained in this answer:

Assume-unchanged (..) is not a promise by Git that Git will always consider these paths are unmodified---if Git can determine a path that is marked as assume-unchanged has changed without incurring extra lstat(2) cost, it reserves the right to report that the path has been modified (as a result, "git commit -a" is free to commit that change).

Is there anything I can do to make git ignore future changes to this file?

Upvotes: 1

Views: 753

Answers (2)

Jeff Puckett
Jeff Puckett

Reputation: 41091

You should be able to stop tracking changes on the file with:

git update-index --skip-worktree <file>

And you can resume tracking changes with:

git update-index --no-skip-worktree <file>

Upvotes: 5

AnoE
AnoE

Reputation: 8355

I believe there is no way to do exactly what you want.

But how about changing the application logic? I.e., instead of checking the content, check for existance. If the file does not exist, your app has not been run before. Remove it from the repository and add it to .gitignore. On first run, let your application create the file with whatever content you need.

Upvotes: 1

Related Questions