1

Is it possible to have a file that is part of the Git repo and created locally (with git clone) but where the contents are not tracked?

Here's my example. I have the file MyFolder\DummyFile.txt with contents:

This file will not be tracked by Git when its contents change.

The file is added to the Git repo:

$ cd MyFolder
$ git add DummyFile.txt
$ git commit -m "Dummy file"
$ git push

A .gitignore is created in MyFolder with the line:

DummyFile.txt

What I expected is that the contents of DummyFile.txt could be freely modified without being notified of changes with git status. But whenever I change the file, Git reports it is modified and needs to be staged.

Note: I still want the file to be created locally on git clone or git pull (if it doesn't exist locally), but once it exists, I'd like Git to forget about it.

Update 1

Comments suggested looking at this question recommending assume-unchanged, and a later comment suggested this question recommending skip-worktree.

Have now tested both in a local Windows Git repo and then in the same repo inside an Ubuntu VM. I am able to confirm that both of the following commands do what I need:

git update-index --assume-unchanged path/DummyFile.txt
git update-index --skip-worktree path/DummyFile.txt

skip-worktree is to be preferred because it has a meaning closer to my intention which is "Git shouldn't track changes to this file, but developers can and are encouraged to make changes to the file".

Note: You must run the command on each instance of the Git repo! When the repo is cloned, the file will be tracked again. The questions mentioned above were not clear on this point.

Update 2

From the comment by @1615903, another question suggests the following elegant solution. The repo has a default file called DummyFile.Default.txt. The developer should rename this to DummyFile.txt locally, and DummyFile.txt is in the .gitignore.

For anyone who comes here, there are two better solutions to assume-unchanged which are closer to the meaning I intended.

AlainD
  • 5,413
  • 6
  • 45
  • 99
  • 2
    This is a common misconception, but it is clearly written in the documentation: "Specifies intentionally **untracked** files to ignore", my emphasis. Untracked, if you have tracked the file, it will always be tracked and any changes to it will be detected by git. You can specifically ask git to ignore changes to it after checking it out, but everybody need to do that. .gitignore does not work on tracked files. – Lasse V. Karlsen Jun 22 '20 at 13:43
  • 1
    Rephrased, without doing something specifically to the file after checking it out, you cannot forget about it, nor can anyone else. Git simply does not have a 100% automatic way to do what you ask. – Lasse V. Karlsen Jun 22 '20 at 13:45
  • 2
    The suggested method is to commit a template to your repository, that you track and handle changes to as normal, but then locally you use a build script or whatnot to detect that you're missing the real file, and then make a copy from the template. – Lasse V. Karlsen Jun 22 '20 at 13:45
  • 2
    Be aware that contrary to all message telling you to use `assume-unchaged` option, the right option is `skip-worktree` https://stackoverflow.com/a/13631525/717372 – Philippe Jun 22 '20 at 14:34
  • @Philippe: Agreed. The question marked as a duplicate is the wrong answer. Use skip-worktree instead of assume-unchanged. Thanks! – AlainD Jun 22 '20 at 14:53
  • 1
    @Melebius: In my opinion, you've marked the wrong answer as a duplicate. I prefer the answer suggested by Philippe (using `skip-worktree`) which is cleaner and closer to my intention. – AlainD Jun 22 '20 at 14:55
  • 2
    [This question](https://stackoverflow.com/questions/3319479/can-i-git-commit-a-file-and-ignore-its-content-changes) includes pretty much all options you have – 1615903 Jun 23 '20 at 05:28
  • 1
    @1615903: That question also mentions a particularly attractive solution which is to have a `DummyFile.Default.txt` in the Git repo which **is** tracked. The developer then renames this to `DummyFile.txt` locally (which is in `.gitignore`). Well spotted, thanks. – AlainD Jun 23 '20 at 05:59
  • _“In my opinion, you've marked the wrong answer as a duplicate.”_ I _suggested_ a duplicate and don’t have the power to modify it on a closed question. You should ask @phd who can do it, see [What is the course of action if I find a better duplicate for a question already closed as duplicate?](https://meta.stackexchange.com/q/315265/217657) for details. – Melebius Jun 23 '20 at 09:06
  • @Melebius: I'll check that link out, thanks. I saw your suggested duplicate, found `skip-worktree` and after testing concluded another answer was a better duplicate, but when I returned to this question found it had already been closed! – AlainD Jun 23 '20 at 09:11

0 Answers0