A buddy and I are developing a Django app and are using git.
As we work, we make fake accounts on our site, login, and upload content to the database, etc..for testing purposes. Every time we merge branches, we get merge conflicts in our database file. The database file is in the repository, and, since we're testing separately, the local copies of the file develop differently.
How do I prevent the database file from being tracked, so we can each keep our local copies?
With the following, we've been able to avoid using a local path:
## settings.py
from os.path import dirname, join
PROJECT_DIR = dirname(__file__)
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': join(PROJECT_DIR, 'foo.db'),
'USER': '',
'PASSWORD': '',
'HOST': '',
'PORT': '',
}
}
What would be ideal, is something like:
## settings.py
from os.path import dirname, join
PROJECT_DIR = dirname(__file__)
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': join('../../../', PROJECT_DIR, 'foo.db'), # this path is outside the repository (ie, 'Users/sgarza62/foo.db')
'USER': '',
'PASSWORD': '',
'HOST': '',
'PORT': '',
}
}
How can we keep our database files from being committed?
Add your database file to .gitignore
. Then you can keep it in its current location, but it will not be under version control.
First off, you'll want to remove the database file from your git repository.
git rm <database_file>
To prevent the file from being added to your repository, create a file named ".gitignore" inside your checkout of the repository, add the database file to .gitignore, and add .gitignore to your repository. (Documentation)
To prevent conflicts with settings.py, I also add settings.py to .gitignore. I then create a file called "settings.production.py", which contains all of the settings for the production server, and add it to the repository. On my local checkout, I simply copy this file into settings.py and modify variables as needed. On my production server, I make a symlink to settings.production.py.
ln -s settings.production.py settings.py
WARNING:
http://example.com/.git
is accessible.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With