3

I have a website under version control using GIT. I have a system set up that essentially, automatically deploys changes in my master branch to my production server. Namely, I have a web-hook in my repository that triggers a PHP script that essentially initiates a git pull on the server in order to pull down changes.

My issue lies in the fact that I currently have a configuration file (config.php) that is tracked using git. Myself and another developer want to have our own config.php files that are different to the file on the server, therefore we want to tell git to stop tracking this file and just pretend it isn't there.

We decided first to test using a less important file, so we added test.php to our .gitignore file, hoping this would do the trick. Git was still tracking the file so I tried running git rm --cached test.php in an attempt to stop tracking the file. This seemed to work so we committed and pushed our changes.

To my surprise, when our deployment script initiated the pull on our server we found that this file had been deleted from production. Thanksfully, the deleted test.php was not important.

TLDR; How can I tell git to ignore a file it currently tracks, have it untracked and deleted from my repository, but leave it intact on my production server?

Jonathon
  • 15,873
  • 11
  • 73
  • 92
  • see https://stackoverflow.com/questions/57418769/definitive-retroactive-gitignore-how-to-make-git-completely-retroactively-forg/ – goofology Aug 11 '20 at 19:34

1 Answers1

6

If you delete a tracked file from the repository, it will be deleted if that change is pulled. That's expected and desired behaviour, since if a file is deleted from the repository, it is assumed that it isn't needed anymore.

The easiest solution in your case would be do delete the file from the repository and add it to .gitignore, and then immediately manually add it back on the server. You could also make some immaterial change to the file on the server first, like adding an empty line, to cause the pull to fail.

Another option is to leave the file in the repository and use git update-index --assume-unchanged config.php on your development repositories. This tells git to ignore changes to the file in spite of it being tracked.

Yet another option is to create a new file like local_config.php that is not tracked by git and conditionally included by config.php if it exists. You could do all your local modifications in this file.

The last option has the advantage that you can still modify config.php on the server by pushing a new version of the file to the repository.

Sven Marnach
  • 574,206
  • 118
  • 941
  • 841