244

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,110
  • 4
  • 33
  • 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 Путлер Капут 六四事 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

3 Answers3

378

Sure.

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

To undo and start tracking again:

git update-index --no-assume-unchanged [<file> ...]
ErezSo
  • 13
  • 3
  • 2
Nick Veys
  • 22,622
  • 4
  • 42
  • 62
  • 6
    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
  • 7
    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
  • 18
    @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
  • 2
    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
  • 1
    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
  • 6
    @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
  • 1
    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
10

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
  • 56,766
  • 33
  • 241
  • 361
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
  • 2