0

I have a file which I have marked assume-unchanged in my checkout. This is respected by most commands (e.g. diff, add ., commit -a), but for some reason this command seems to be ignoring the flag:

git commit .

Is this a bug? If not, is there a flag to change git's behaviour in this case?

(My git version in 2.25.1, should that be important.)

Alice Purcell
  • 12,622
  • 6
  • 51
  • 57
  • 3
    You need `--skip-worktree`, not `--assume-unchanged`. – CodeCaster Dec 15 '21 at 11:18
  • 1
    @CodeCaster: `--skip-worktree` might not help either, especially when using sparse checkout (which uses the skip worktree bit for its own purposes). This whole scheme is not supported: if it works for any particular users, that's fine for them, but they should not count on it working in future Git versions. – torek Dec 16 '21 at 02:50
  • @torek good warning, thanks. I'm all in favor of including template files like `.env.dist` or `appsettings.dist.json`, having developers copy that into a .gitignored `.env` or `appsettings.json` so people don't have to do tricks to keep the configuration file ignored until a change comes in. But sometimes you need a workaround because the repo and/or team aren't ready for that - but as you say, YMMV. – CodeCaster Dec 16 '21 at 09:04

1 Answers1

3

The assume-unchanged flag to Git is used to improve performance on systems with slow lstat(2) calls. When you set this bit, you promise not to change the file at all. If you are changing the file, then you must unset the bit to notify Git that it's changed. This is a performance optimization only.

If you are trying to ignore changes to tracked files, the Git FAQ is very clear that's not possible:

Git doesn’t provide a way to do this. The reason is that if Git needs to overwrite this file, such as during a checkout, it doesn’t know whether the changes to the file are precious and should be kept, or whether they are irrelevant and can safely be destroyed. Therefore, it has to take the safe route and always preserve them.

It’s tempting to try to use certain features of git update-index, namely the assume-unchanged and skip-worktree bits, but these don’t work properly for this purpose and shouldn’t be used this way.

The FAQ recommends an approach for configuration files:

If your goal is to modify a configuration file, it can often be helpful to have a file checked into the repository which is a template or set of defaults which can then be copied alongside and modified as appropriate. This second, modified file is usually ignored to prevent accidentally committing it.

bk2204
  • 64,793
  • 6
  • 84
  • 100