Jason
Jason

Reputation: 23395

Git - How to simultaneously add a file to .gitignore and still keep it in Git / the remote repo?

I have a database configuration file, database.yml, which needs to be in Git because my clients build server clones the entire repo for testing.

However I also have a copy of this file on my local machine for development, the problem is I want to make changes to this locally and have git ignore these changes i.e. not commit the changes to the repo when changes are made and not overwrite it / give me hassle about it being different when I issue a pull request.

Does anyone know how this can be achieved? Thanks!

Upvotes: 1

Views: 243

Answers (1)

user229044
user229044

Reputation: 239302

You shouldn't be including your real database credentials in version control. Copy your database.yml file to database.yml.example with a blank username/passwword, and add database.yml to .gitignore. Each time you clone, copy database.yml.example to database.yml and put that installation's specific database credentials into the database.yml copy.

If you want to avoid having to re-setup your production environment each time you deploy, use something like Capistrano which will let you keep things like config files between deploys, or write your own script which creates a link to a persistent database.yml outside the deploy directory.

You can also use git update-index --assume-unchanged config/database.yml to cause Git to ignore local changes to that file, but you shouldn't do this. The correct solution is to keep your real database credentials out of Git.

Upvotes: 4

Related Questions