21

I have a file with database settings in my project which I have set to some defaults. The file is tracked by Git and checked in. Since this file will be edited with different values various developer machines, is there a way I can tell Git to ignore new changes to this file?

I tried adding the file to the .gitignore file, but since the file is tracked it isn't ignored. This is alright and good in other situations, but I am wondering if there is something I can do here?

Eugen Konkov
  • 22,193
  • 17
  • 108
  • 158
  • 1
    Read [here](http://gitready.com/intermediate/2009/02/18/temporarily-ignoring-files.html) for one option. You may tell Git to temporarily ignore changes to this file. But in general, it isn't such a good thing to have files which are both treated as versioned and unversioned at the same time. – Tim Biegeleisen May 09 '18 at 11:27
  • 1
    Possible duplicate of [Can I 'git commit' a file and ignore its content changes?](https://stackoverflow.com/questions/3319479/can-i-git-commit-a-file-and-ignore-its-content-changes) – 1615903 Nov 25 '18 at 15:31
  • @TimBiegeleisen: I do not want see local changes to configuration file. But in general it should be tracked and if new configuration options are implemented I can add them via `git add -p`. But I do not want to see those local changes every time when `git status` – Eugen Konkov May 29 '20 at 07:39
  • Did you really just look at this question now, two years later? – Tim Biegeleisen May 29 '20 at 07:45
  • @TimBiegeleisen: Yes. SO notify me about some changes here. So I look into – Eugen Konkov May 29 '20 at 08:54
  • Does this answer your question? [Git: Ignore tracked files](https://stackoverflow.com/questions/10755655/git-ignore-tracked-files) – Nerower Mar 31 '23 at 17:26

3 Answers3

20

One may use git update-index --skip-worktree FILE for this purpose.

It is similar to --assume-unchanged but, unlike --assume-unchanged, is not just an unpredictable performance trick.

To cancel --skip-worktree effects and unset the flag use --no-skip-worktree.

Downsides

  • This flag is for local repository only and cannot be pushed to the remote. Thus every clone that needs to ignore the file should do it manually.
  • You may face conflicts when switching to another branch or using git pull. (more details)
AXO
  • 8,198
  • 6
  • 62
  • 63
  • 1
    After `--skip-worktree` I was not available to switch between branches because of untracked changes. – Nike Kov Jun 20 '22 at 13:46
17

I recommend naming the source-controlled file differently than its actual expected name. For example, if the file is normally named config.json, then name your example file config.json.dist and commit this file. Then add config.json to your .gitignore file. Your devs would simply cp config.json.dist config.json after cloning, and then edit it as required, making subsequent commits without having to worry about accidentally changing the default file or forgetting to toggle some setting on and off all the time.

You might even edit your code to search for config.json first, and if that doesn't exist, fall back to config.json.dist. This would allow the devs to work without even performing the copy step. (This is how PHPUnit works.)

Alex Howansky
  • 50,515
  • 8
  • 78
  • 98
  • Would be nice to have git option to ignore changes to `this file`: `git config current_repo.ignore=list,of,files,to,ignore` – Eugen Konkov Aug 19 '20 at 16:13
3

You can use git update-index --assume-unchanged <file>

And if you want to track it again use git update-index --no-assume-unchanged <file>

Corba
  • 309
  • 1
  • 10