Reputation: 5647
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?
Upvotes: 288
Views: 89681
Reputation: 798
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.
Upvotes: 48
Reputation: 66741
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.
Upvotes: 13
Reputation: 23939
Sure.
git update-index --assume-unchanged [<file> ...]
To undo and start tracking again:
git update-index --no-assume-unchanged [<file> ...]
Upvotes: 437
Reputation: 75
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
Upvotes: 5