270

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?

haymansfield
  • 5,419
  • 4
  • 34
  • 51
  • 10
    possible duplicate of [git: can i commit a file and ignore the content changes?](http://stackoverflow.com/questions/3319479/git-can-i-commit-a-file-and-ignore-the-content-changes) – Ciro Santilli OurBigBook.com Feb 20 '15 at 15:49
  • 5
    If these files are automatically **generated** by the build, then they should not be tracked in git. – Codebling May 17 '15 at 03:27
  • 1
    Here's a quick tutorial on gitignore that you might find useful: https://learningpassion.wordpress.com/2016/06/17/git-tutorial-day-to-day-use-part-4-gitignore-diff-and-difftools-moving-and-removing-files/ – joker Jun 26 '16 at 12:43

4 Answers4

412

Sure.

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

To undo and start tracking again:

git update-index --no-assume-unchanged [<file> ...]
ErezSo
  • 15
  • 1
  • 2
Nick Veys
  • 23,458
  • 4
  • 47
  • 64
  • 8
    What happens to files in this state when if I pull in modifications to them? – haymansfield May 31 '12 at 14:17
  • 3
    @haymansfield the help page for the command says the following ```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.``` – Parham Sep 02 '14 at 12:33
  • 1
    @Parham, the documenation is now being changed to be more clear about the intent of the --assume-unchanged flag because of user misunderstandings. In Git, loosing data would be ungraceful, thus extra objects which save the data is thought 'graceful'. That quote will be changed. – Philip Oakley Dec 11 '14 at 15:40
  • 8
    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. – Gauthier Apr 24 '15 at 08:33
  • @Gauthier, add it to the repo and then add a `.gitignore` file that specifies the desired file(s) to ignore. I'm not 100% that that'll work, but I would think that it would. – Tyler Crompton Nov 10 '15 at 18:45
  • 21
    @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". – Jon V Jan 20 '17 at 22:01
  • 3
    There is a nasty catch with this. If you have modified such a file, then you try to switch branches, git will say nuh-uh, you have changes that would be overwritten if you did that. So you go to git stash, but of course you have nothing to stash. I don't know if there's any way around this other than starting tracking again. – see sharper Feb 08 '17 at 05:57
  • @seesharper I do not have that issue. What version of git are you using? I have: git --version 2.7.1.windows.2. Maybe the issue you're referring to only happens if the version in the repo has been updated in the other branch, in which case it's good that you're alerted to the change. – carlin.scott Mar 22 '17 at 19:31
  • 5
    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 Nov 15 '18 at 13:23
  • 8
    @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. – Philippe Carphin Feb 13 '19 at 22:17
  • @JonV I am encountering a similar use case in my project. Maybe consider using a generator script to generate the template. – Daniel Chin Aug 14 '20 at 03:33
  • 2
    There is an alternative `--skip-worktree` flag instead of `--assume-unchanged` in case the file can be modified by others. More here https://stackoverflow.com/questions/13630849/git-difference-between-assume-unchanged-and-skip-worktree – Krym Feb 18 '21 at 10:17
  • Is possible to assume-unchanged by extension rather than by single file? I tried putting *.extension instead of the path and it didn't work. The single file path worked properly – ff8mania Aug 14 '22 at 22:00
  • And then how do you view the files which were git assume-unchanged ? – shargors Dec 21 '22 at 21:20
26

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.

Gerhard
  • 518
  • 5
  • 9
11

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.

rogerdpack
  • 62,887
  • 36
  • 269
  • 388
4

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
koct9i
  • 65
  • 1
  • 3
  • 22
    Could you give more details on what this means and what it's doing by chance? – rogerdpack Jan 30 '17 at 16:52
  • this adds filter for file which reads content from ".orig" file which has original text – koct9i Jan 06 '19 at 19:09
  • From https://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._ – danissimo Mar 27 '23 at 11:34