6

I am attempting to use git to manage deployment to my live website. The problem that I'm having is that I have a couple of settings files that I don't want to be updated when I push to production

what I'm looking at doing is either using a hook or smudge/clean to change the file contents for example from

<?php
define('DB_NAME', 'live');
define('DB_HOST', '127.0.0.1');
define('DB_USER', 'live_user');
define('DB_PASS', 'livePass');

to

<?php
define('DB_NAME', 'local');
define('DB_HOST', '127.0.0.1');
define('DB_USER', 'local_user');
define('DB_PASS', 'localPass');

Is there anyone who could talk me through the process please

I did wonder about using post-receive hook and a shell script to replace the contents, but ideally I want the contents in the repo to be changed before I run git checkout -f not changed in the live copy after

James A Mohler
  • 11,060
  • 15
  • 46
  • 72

2 Answers2

7

ideally i want the contents in the repo to be changed before i run git checkout -f not changed in the live copy after

The closest is a filter content driver which will replace the value at the git checkout.

smudge

(from Scott Schacon's Pro Git book page on Git Attributes: section "Keyword Expansion")

So in your case: a smudge filter, declared in a .gitattributes file.

See "Can git automatically switch between spaces and tabs?", except you would use a sed to replace local to live (as in this example)

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • thanks for the quick reply, so to clarify is there any specific script type id have to use or will it run anything that my server will run? if thats the case how does this interact with the files in the repo, i dont understand the relationship and therefore what i have to do to update the files – Matt Indeedhat Holmes Jan 11 '13 at 16:49
  • @MattSmokey-watersHolmes you can declare a script or use a command directly in your smudge declaration. In your example, a simple `sed` would be enough. – VonC Jan 11 '13 at 18:36
  • 1
    @Nicholas attribution added. – VonC Aug 05 '13 at 08:22
  • You were fast! I cancelled the downvote :-) Stealing images *with* attribution is fine by me. – Nicholas Shanks Aug 05 '13 at 08:25
0

I think, you have to read topic Customizing Git - Git Attributes in Git Book from start to end in order to have full picture, but especially carefully inspect part "Keyword Expansion", where custom hand-made scripts used to coverting KEYWORD in repository to FINAL TEXT in Working Directory (and in back). They are any code, which can be executed on host with Working Directory, referenced from .gitattributes file

Note: Because you have to have different clean-filter output (for local and live), you may have two slightly different (in content) clean scripts on local and live WorkingDir under common filename (in book's sample clean result in equal for all locations - single script used).

Lazy Badger
  • 94,711
  • 9
  • 78
  • 110