42

I'm using dotenv for my rails project to store API keys. I have the dotenv gem in my gemfile, created a .env in the root of my app and have my API keys listed in there. However, when I push up to github, I notice that the .env file is still there. Am I missing a step? How can I hide the .env file from being uploaded to github.

Thank you!

Ryan_DS
  • 750
  • 7
  • 28
dace
  • 5,819
  • 13
  • 44
  • 74

7 Answers7

65

Create a file called .gitignore in the root directory of your project (it's possible one is already there) and add .env on its own line in the file.

Also, if you have already added it to your repository, then you will have to add the file to .gitignore as stated above and then clear Git's cache. You can find information on that in the following answer on StackOverflow.

Community
  • 1
  • 1
searsaw
  • 3,492
  • 24
  • 31
  • 4
    Clearing the cache was exactly what I needed. Added it to the .gitignore, but it was still showing up. Thank you! – dace Jun 07 '15 at 18:41
  • 4
    probably goes without saying, but you should probably change any keys that were in that .env file on github :) – Jed Schneider Jun 07 '15 at 19:06
16

just add .env to the .gitignore file at the root of the project.

if still, you can see the .env file in git changes, you need to prevent tracking this file from git by executing the below command

git rm --cached .env
adel parsa
  • 236
  • 2
  • 5
12

create a file .gitignore in your root directory and add .env file name in that .gitignore folder.After that You must untracked the .env file from git using git rm --cached .env and following that use 1. git add. 2. git commit -m " " 3. git push -u origin master . Now your .env file is remove from github repo.

Atif
  • 121
  • 1
  • 4
7

You can exclude files from the repo with a .gitignore file: http://git-scm.com/docs/gitignore.

Andy Waite
  • 10,785
  • 4
  • 33
  • 47
6

If u have already sent it to git hub, you can remove it by writing the following commands

git rm -r --cached .env

git add .
git commit -m 'your message'
git push

it will remove it automatically

Arinzehills
  • 1,491
  • 10
  • 14
3

If before you did git init and then some commit push your project. now you realize that remove environmental variables for security purposes.

1st try: create .gitignore in your root project then include your file which want to ignore in GitHub repository.

if 1st one do not work ,you can try this

git rm --cached .env

git add .

git commit -m "Removed.env"

git push

0

In addition to this, the gitignore will not push your sensitive data to your version control that is why when you do an ls or when listing all the files that you have in your root folder git ignore doesn't appear unless you do ls --all when inside your gitignore files add the env file by typing .env inside the gitignore