Mellgood
Mellgood

Reputation: 325

How to gitignore a file on remote branch git

I have a branch where I have a file called keys.py. The file contains some private keys I do not want to share with other on my repository, but I'd like to leave a file called keys.py inside the repo to be used like a template. I also need to avoid tracking other people's changes to key.py.

I tried to push the template file keys.py and then I created a .gitignore file to ignore keys.py but it seems like it has no effect...

I am missing something but i can not figure out how to solve my problem... any hint?

Upvotes: 1

Views: 711

Answers (3)

Murilo Cruz
Murilo Cruz

Reputation: 2551

As far as I know, the common approach for this problem is to have a keys.py.example, and when someone clones the repo, copy it to keys.py and change the values as necessary (you can write it on the README). You can do it with this steps:

  • Run git rm keys.py, so the keys.py that you have on the repo gets removed
  • Create or add the line keys.py on your .gitignore file
  • Create keys.py.example with the default values
  • git add . to add all this changes on the commit
  • git commit to commit the removal of the file from the repo, the example file and the gitignore changes

Usually this kind of values goes on a .env file instead of keys.py. I can see you are using python, so you will need to use this package.

Upvotes: 2

Mellgood
Mellgood

Reputation: 325

Many thanks for your replies! I solved the problem in a tricky way:

  1. Renamed key.py into key_template.py
  2. Added key.py into .gitignore
  3. Added to the readme the instruction to create key.py from key_template.py

Upvotes: 0

AnoE
AnoE

Reputation: 8345

Yup, .gitignore has no effect on paths that already have been added to the repository.

For me, the best solution for your problem is to have the real keys.py outside of the repository, say in a file like $HOME/.appname/keys.py. The question then is how your application finds this file. A good possibility:

  • Configure the file name in an environment variable.
  • Have the application look into $HOME/.appname/keys.py as a default.

Upvotes: 2

Related Questions