0

To be clear right up front, since there are many similar-sounding questions around: I DON'T want to track the file.

My use case is that I have a "template" file that developers need to fill in with information. I don't want developers to go committing their changes to this file - they get the file on clone, make their own local changes, and those changes are just that: local. However, I'm not really sure how to get the file into my repo in the first place. Also, I might want to make occasional changes to the template file, but I'd prefer that I have to jump through hoops to do so, so that neither I nor anyone else accidentally commits their secret info.

How can I make commits to an un-tracked file in a git repo?

Him
  • 5,257
  • 3
  • 26
  • 83
  • You can't. For a file to be distributed with the repo, it has to be tracked by the repo. – fredrik Apr 08 '22 at 16:11
  • 2
    If that would be an option for you, you can have two different files - let's say service-template.yml and service.yml and add the real service.yml into the .gitignore file to avoid being accidentally commited. But it would require to instruct the devs to do a copy after cloning the repository – VANAN Apr 08 '22 at 16:32
  • To address the close vote: This is reproducible and is not caused by a typo. Even if the solution to this is ultimately "You can't", then this will definitely help future readers. I personally spent some time googling for how to do this before I posted here. If the first result when searching is "You can't", then this is valuable information. – Him Apr 08 '22 at 16:41
  • Take a look at https://stackoverflow.com/questions/18276951/how-do-i-stop-git-from-tracking-any-changes-to-a-file-from-this-commit-forward – nbstrat Apr 08 '22 at 19:00

2 Answers2

1

Git only allows you to commit files that you track. Tracking a file means, specifically, making Git care about it for commits. If the file is not tracked, then it cannot be committed.

If your goal is to create a template file that people then fill in, then create the template file in a different location from the one where it needs to exist when in use, and use a script to fill it in and copy it to the right location. Then, ignore the path for the modified file. That way, nobody will accidentally commit their file because it's ignored.

What many people try to do is ignore changes to tracked files using some variant of git update-index. The Git FAQ is clear that doesn't work and shouldn't be done. Instead, the Git FAQ recommends the approach I mentioned above, where you create a template file in a different location and ignore the modified file.

bk2204
  • 64,793
  • 6
  • 84
  • 100
0

Take a look at this SO question and the referenced github link as it may help address what you are wanting to accomplish.

How do I stop Git from tracking any changes to a file from this commit forward?

https://help.github.com/articles/ignoring-files#ignoring-versioned-files

nbstrat
  • 108
  • 6
  • The [Git FAQ is clear that you cannot ignore changes to tracked files](https://git-scm.com/docs/gitfaq#ignore-tracked-files). – bk2204 Apr 08 '22 at 21:08