I have a github repo which is being used by many users, and assuming more people will join as the project grows. As everyone clones the project i want it to be seamless.
It is a python project, and i have a
.env file for passing the env variables. The file has some variables and some credentials.
I want the
.env file to be untracked as some of the credential fields are distinct for every user and if anyone changes it on local it will be pushed via
git add ..
I tried using
git rm --cached but it removes the file from the github repository.
The closest answer i got is using
git update-index --assume-unchanged .env. But this command will work on local only, if anyone else clones the repository the file will still be tracked.
To summarize, I want to keep the file in the repository, but it should not be tracked by Git.
Any Help Is Appreciated.
The answer is that this isn’t something you can do with Git easily.
For example, if you add the file to
gitignore and then
git add --force .env, changes to that file will still be shown in
git status (see e.g. How do I make Git forget about a file that was tracked, but is now in .gitignore?). Git just is not set up to include a file in the repository but ignore changes/updates to that file. It’s theoretically possible within the underlying data model, but Git itself does not support it.
However, a common workaround is to create a template or example
.env file (e.g.
env.example) that you instruct users to copy to
.env when they first set up the project. You would track
env.example in the repo, and put
You can even provide a
scripts/setup.sh or similar that can do this for them. Here’s an example that would work well with a variety of Python projects:
#!/bin/sh set +x cp -nv env.example .env pyenv install --skip-existing pyenv exec python -m pip install -U pip pyenv exec pip install wheel pyenv exec pip install --requirement requirements-dev.in pyenv exec pip install --editable .
Another option would be to generate the example file from a template in code:
#!/bin/sh set +x if ! test -f .env; then cat > .env <<ENV FOO=1 DB_HOST=localhost DB_USERNAME=dev DB_PASSWORD=dev1234 ENV fi pyenv install --skip-existing pyenv exec python -m pip install -U pip pyenv exec pip install wheel pyenv exec pip install --requirement requirements-dev.in pyenv exec pip install --editable .