1

I was new to Git and had added some files that I don't want tracked (a config file and some caching files). I am familiar with the process of adding the file to .gitignore and then running git rm --cached <file> so that it removes it from git but keeps the local file.

Then when I push this change and pull on remote it removes the file on remote. Since this is a config file that needs to be there, is there any solution that will basically apply the --cached option when doing a git pull? Or does this just have to be done manually?

  • You can absolutely do this, see https://stackoverflow.com/questions/57418769/definitive-retroactive-gitignore-how-to-make-git-completely-retroactively-forg/ – goofology Aug 11 '20 at 19:33

2 Answers2

1

You cannot do this. If the file needs to exist in the repo, you need to keep in the repo. The better solution is to use some other tool to manage the config file, and not commit a file that is meant to be different in different instances of the repo.

Typically you would provide something like file.config.example, which contains placeholder values. Each of your cloned instances of the repository would copy file.config.example to file.config, which would be included in .gitignore to prevent specific config files from being comitted.

user229044
  • 232,980
  • 40
  • 330
  • 338
0

I'm afraid it has to be done manually, but you can pull the file out of the old revision using git itself.

git checkout HEAD^ -- file.config

Should copy the file called file.config out of the commit before HEAD. Obviously make sure it does what you want on a test repo before trying it in production :).

Andrew Aylett
  • 39,182
  • 5
  • 68
  • 95
  • Would it be possible to do this recursively (say there are configuration files like this in every directory)? – hal7df Jul 29 '15 at 03:33