0

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.

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
anantdark
  • 387
  • 1
  • 2
  • 12
  • 3
    Can you use a tracked sample file, i.e. `example.env` and instruct users to complete the variables with their values and rename to `.env`? This is the way many popular repositories handle this type of situation. – esqew Aug 14 '23 at 19:18
  • Are you looking for `.gitignore`? – Benjamin W. Aug 14 '23 at 19:20
  • 5
    That is a contradiction in terms. Cloning a repository means getting all the files that are tracked, and not any that are untracked. "Untracked" means that a file never makes it to upstream; that's the point of tracking. Also, this is not a Python question. Please only tag languages that are relevant to understanding and solving the problem - do not tag them simply because your project happens to involve them in some way. – Karl Knechtel Aug 14 '23 at 19:20

1 Answers1

3

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 .env in gitignore.

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 .
shadowtalker
  • 12,529
  • 3
  • 53
  • 96
  • 1
    Adding comment for anyone who visits this question: https://stackoverflow.com/questions/1274057/how-do-i-make-git-forget-about-a-file-that-was-tracked-but-is-now-in-gitignore?rq=1 – anantdark Aug 14 '23 at 19:33