9

How would you go about setting up this scenario in git:

My source has a settings file with configuration settings such as db connection credentials, etc... (this is a Drupal source and I'm referring to settings.php)

When developers clone the source, they'll need to go in and change settings specific to their environment. These changes of course should not be pushed back to origin. And at the same time I want them to be able to work with this default template (since most of it will not be changed).

So .gitignore doesn't work here because I want it in their first clone. Do I need to teach every new developer about git update-index --assume-unchanged?

Isn't there a slicker way to do this?

ack
  • 14,285
  • 22
  • 55
  • 73
  • possible duplicate of [Committing Machine Specific Configuration Files](http://stackoverflow.com/questions/1396617/committing-machine-specific-configuration-files) – Senseful Aug 15 '14 at 01:01

3 Answers3

8

I would rename database.php to database.php.sample and add database.php to .gitignore.

Whenever a new user comes up, just copy from database.php.sample to database.php and make the appropriate changes.

Pedro Nascimento
  • 13,136
  • 4
  • 36
  • 64
  • Good suggestion! Just like `wordpress` did. :D – Kjuly Dec 21 '11 at 04:15
  • 1
    Thanks! People in my current project are kinda against this, but I tend to think this is the best approach. – Pedro Nascimento Dec 21 '11 at 04:29
  • 1
    You should be wary of ignoring files which used to be tracked in the project in the past. git regards ignored files as disposable, so doing `git checkout && git checkout -` will delete your `database.php` file without warning if `database.php` was tracked in ``. A safer option is to have `database.php` contain a set of default options, but at the end of that file include `database.local.php` (if it exists) which can override the default settings. – Mark Longair Dec 21 '11 at 07:31
0

A slicker way to do this would be to use a filter driver, with a smudge script:

content driver

Building on Pedro's proposition (with database.php in .gitignore), you would:

  • version a database.php.sample
  • copy it to a private (ie not versioned) database.php through the smudge script (on git checkout), only if said private database.php doesn't exists yet.

As of git 1.7.4, %f in a filter definition will be replaced by the current file's path; see the .gitattributes manpage

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • The idea of a **filter driver** is to handle automatically the copy of `database.php.sample` instead of having to copy it manually. – VonC Dec 21 '11 at 05:18
0

Yes there is. This is a solution I already posted.

Use git add -N on database.php then git add -p. Edit the hunk so as to replace sensitive data with placeholders.

The one thing you need to be careful afterwards is that you should never git add . from the root of the repository -- but nobody does that, right? ;)

fge
  • 119,121
  • 33
  • 254
  • 329
  • Please describe your solution more detailed. I tried the following: `git add -N template.file` Cleaned up the template file `git add -p template.file` `Stage this hunk [y,n,q,a,d,/,s,e,?]? y` commit the stage with Sourcetree After that any change of `template.file` is still noticed and would be committed :( – user1185087 Aug 29 '18 at 08:27