0

I'm using Git to manage a CodeIgniter project. I work on my local machine and push to a remote server, where I test everything. Then I use git ftp push to update the production site on another host.

Now, to make this work I need three different index.php files, one for every environment, where I just set a different environment variable.

So I need to gitignore the index.php file, and even git-ftp-ignore it. For the ftp it is working. For the normal gitignore it isn't: It keeps updating the index.php file on the remote server.

I tried doing git rm --cached index.php, but what I get is that it completely deletes the index.php file on the remote server.

What am I doing wrong?

Edit: It seems it's not something I can do with git, but I've found a better way to set the CodeIgniter environment variable.

Paŭlo Ebermann
  • 68,531
  • 18
  • 138
  • 203
Carlo
  • 3,644
  • 6
  • 38
  • 59

2 Answers2

3

Firstly, the ignore mechanism doesn't have any effect on files that are already being tracked in the repository. So, if you want the .gitignore to work, you have to remove the file from the repository (as you did, with git rm --cached) but then, as you observed, it'll be deleted when you update the remote repository.

In summary, I don't think you should .gitignore the file in this situation. There's a subtlety about the semantics of ignored files in git which has caught me out number of times - ignored files are regarded as disposable, since the mechanism is designed for build products. What you have here is a file that is you want to be ignored, but is precious, and git currently doesn't have a way of specifying that.

For example, in your situation, if you recreate index.php manually, that will appear to all work fine, but then if you use git checkout old-commit to move back to an old version where index.php was tracked, and then go back to where you were before, with git checkout -, your index.php will be deleted!

I would suggest that you change your index.php to source the right setting for the environment variable from some other file which is either:

  1. outside the repository
  2. or is in the repository, ignored, and at a path that has never been tracked in the repository

Ideally, then also add a default value for when that other file doesn't exist.

Mark Longair
  • 385,867
  • 66
  • 394
  • 320
  • 2
    thanks for your answer. I understood that it is not something I can solve with git. So, searching around for something else I've found this: http://philsturgeon.co.uk/news/2009/01/How-to-Support-multiple-production-environments-in-CodeIgniter – Carlo Aug 24 '11 at 14:51
0

This is an old question, but I do this as well and have struggled with it in the past.

What is happening is that since index.php wasn't originally in your .gitignore, index.php is being tracked by all of your repositories.

When you updated the .gitignore on your development site and removed it from your repository, what it does is passed the "deleted index.php" to your production site. If your production site is still missing index.php on ITS .gitignore, it will "delete" index.php as it should.

What you then have to do is ensure you add index.php to every .gitignore on every branch of every repository. THEN you can use rm --cached and everything will work as expected.

Darrrrrren
  • 5,308
  • 5
  • 28
  • 49