Skip to content

Instantly share code, notes, and snippets.

@AhmadHRai
Created January 15, 2024 14:54
Show Gist options
  • Save AhmadHRai/6451df55d47647d679a3ed4f06634c2a to your computer and use it in GitHub Desktop.
Save AhmadHRai/6451df55d47647d679a3ed4f06634c2a to your computer and use it in GitHub Desktop.
Pushed .env File to Remote? Steps to Remove and Add to .gitignore

If you accidentally push a .env file to a remote repository, you should immediately take steps to remove it and add it to .gitignore to prevent it from being tracked in the future. Here are the detailed steps:

Step 1: Remove the file from your repository's history

Use the git filter-branch command to rewrite the history of your repository and remove the file. Replace PATH-TO-YOUR-FILE with the actual path to the .env file in your repository.

git filter-branch --force --index-filter\
 "git rm --cached --ignore-unmatch PATH-TO-YOUR-FILE"\
 --prune-empty --tag-name-filter cat -- --all

This command rewrites the entire history of the repository to remove references to the specified file. It tells git rm to untrack the file but also keep it in your working directory. The --ignore-unmatch option ensures that the command doesn't fail if the file is absent in some commits. The --prune-empty option removes commits that become empty as a result, i.e., commits that only included changes related to the removed file. Finally, the --all option applies the filter to all refs in the repository, including branches and tags.

Step 2: Prune the non-referenced objects and optimize the repository

After the above step, the commits with the sensitive files are disassociated but still present. To remove these old commits, run:

git for-each-ref --format="%(refname)" refs/original/ | xargs -I {} git update-ref -d {}

Then, run the garbage collection commands:

git gc --prune=now
git gc --aggressive --prune=now

These commands prune the non-referenced objects and optimize the repository.

Step 3: Force push the changes to the remote repository

Since you've rewritten the history of your repository, you need to force push the changes to the remote repository. This will overwrite the history of the remote repository with your local one.

git push origin --force --all
git push origin --force --tags

The first command forcefully pushes all branches to the remote repository, and the second command forcefully pushes all tags to the remote repository.

Step 4: Inform collaborators

If others have cloned or fetched from the repository, inform them about the changes. They will need to re-clone the repository or try to rebase their local changes atop the modified history.

Step 5: Add the file to .gitignore

Finally, add the .env file to your .gitignore file to prevent it from being tracked in the future. Simply open your .gitignore file and add a new line with the relative path to your .env file:

.env

Then, commit and push the updated .gitignore file to the repository.

Remember, rewriting the history of a repository is a serious action. Make sure you understand the implications and have taken necessary measures to avoid losing any important changes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment