302

I have some tracked files in a repository which are automatically modified when building the code. I don't want to untrack them, I just don't want them to appear as modified and I don't want them to be staged when I git add.

Is this possible?

3

4 Answers 4

444

Sure.

git update-index --assume-unchanged [<file> ...]

To undo and start tracking again:

git update-index --no-assume-unchanged [<file> ...]
Sign up to request clarification or add additional context in comments.

What happens to files in this state when if I pull in modifications to them?
Any way to apply --assume-unchanged upon cloning? So that all users would have the files, but not see local changes to them in diffs.
@Tyler - no, once a file is tracked by git it will always be tracked, even if that file appears in a .gitignore. My use case is something like "here's a base template of a file where you'd store your credentials in, now never commit it".
This is excellent until you forget about this and it hits you few years later, after you forget that this command exists and why you have done it in a first place.
@watbywbarif I have the command git update-index --no-assume-unchanged $(git ls-files $(git rev-parse --show-toplevel)) set as an a git alias. Whenever I suspect it's some of these shenanigans I just do that.
59

The accepted answer is not correct. --assume-unchanged only causes Git to skip certain (sometimes expensive) file system checks -- it doesn't guarantee that Git shows the file as "unchanged".

The same command but with the option --skip-worktree, however, does work. So to prevent a tracked but changed file from appearing as changed in the Git status, use

git update-index --skip-worktree [<file> ...]

To undo and start showing it as changed again:

git update-index --no-skip-worktree [<file> ...]

See also the cited email from a Git maintainer in this answer, the Git documentation git-update-index, and FallenGameR's blog entry about how the two react to different scenarios.

14

Another approach (from a now deleted answer by Seth Robertson, but I found it helpful so resurrecting it) is to maintain a "tracked" template file, then have local untracked version of it, ex: "config.sample.ini" or "config.ini.template" see https://gist.github.com/canton7/1423106 for a full example.

Then there won't be any concerns if the file is changed within git, etc. and you can use .gitignore (finally) on the local untracked files.

Comments

5

An another solution using git attributes and %f in filter command:

git config filter.orig.clean "cat %f.orig"
cp filename filename.orig
echo "filename filter=orig" >> .git/info/attributes
echo "filename.orig" >> .git/info/exclude

Could you give more details on what this means and what it's doing by chance?
this adds filter for file which reads content from ".orig" file which has original text
From git-scm.com/docs/gitattributes#_filter: One use of the content filtering is to massage the content into a shape that is more convenient for the platform, filesystem, and the user to use. For this mode of operation, the key phrase here is "more convenient" and not "turning something unusable into usable". In other words, the intent is that if someone unsets the filter driver definition, or does not have the appropriate filter program, the project should still be usable.

Your Answer

Draft saved
Draft discarded

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.