Remove .env From Github Repository (Clean Delete) 

Remove-Env
Make .gitignore Files

The first step is to create a file in the root folder named .gitignore, then write down the files that you don't want to push to the remote repository, such as .env.

Commit and Push to Remote Repository
git add .
git commit -m 'add gitignore file'
git push origin (branch)

The purpose is to add the .gitignore file to the remote repository, which already contains the .env file inside.

Remove a file or directory from the Git index
git rm -r —cached .env

.env will removed in the staging area without removing it from your working directory or your local file system.

Commit and Push to Remote Repository
git add .
git commit -m 'add gitignore file'
git push origin (branch)

When viewed in the GitHub repository, the .env file is no longer present, but it is still visible in the commit history, which is very dangerous (especially if it contains valuable information such as passwords, etc.).

Remove .env from the entire commit history
git filter-branch —index-filter “git rm -rf —cached —ignore-unmatch .env” HEAD

It goes through each commit in the commit history of the current branch, starting from the latest commit (HEAD). For each commit, it removes the .env file (and its contents) from the Git index (staging area) without physically deleting the file from the working directory. This means that the file will be untracked in each commit, and its history will be removed. The result is that the .env file will be removed from the entire commit history of the branch.

git push —force

Is used to forcefully push your local changes to the remote repository, even if it results in a non-fast-forward update. This means that it will overwrite the history of the remote branch with the history of your local branch, potentially discarding any commits or changes that exist only in the remote repository but not in your local repository.