Accidental commits can be tricky to remove with Git. In this GitHub Skills course, you'll use BFG Repo-Cleaner to to change the history of a Git repository. You can apply what you learn in this course to fully remove sensitive material from your own repository.
You removed .env
from the repository's root directory! 🎉
Now that we've deleted the file, people that browse the repository on GitHub.com or anyone looking at just the head commit won't see the file. However, due to Git's nature, the file is still present in the history. In this step, we'll work on removing the file from the repository history.
What is a head commit? In Git, HEAD points to a branch or a commit. When we say head commit, we usually mean the most recent commit in the repository's history.
There are multiple tools available for removing Git history, we'll use BFG Repo-Cleaner in this step. You can find additional documentation on Using the BFG in GitHub Docs.
What is BFG Repo-Cleaner? BFG Repo-Cleaner is software that can help you search through and alter repository history. Git can natively do this using git filter-repo
, but it can be more complex.
- Update the local copy of your repository to ensure you have the most recent version of the course files.
git pull
- Install BFG Repo-Cleaner on your machine. You can follow the instructions on the web site to do so or you can use a package manager for your operating system.
- Confirm the
.env
file is removed from the root directory. The command should return empty.find . -name ".env"
- Search for .env in the repository's history. The command should return at least 2 commits: the addition of
.env
when you copied this template repository, and the removal of.env
.git log --stat --all -- .env
- Use BFG Repo-Cleaner to delete all references to
.env
that exist in the repository.bfg --delete-files .env
- The tool will run and make some suggestions about some follow-up commands. Run those to get your local repository cleaned up.
- Repeat the search for
.env
in the repository's history. This time, the command should return empty.git log --stat --all -- .env
- Push your changes to GitHub. Note we're using the
--force
argument in this step since we're altering Git history.git push --force
- Wait about 20 seconds then refresh this page (the one you're following instructions from). GitHub Actions will automatically update to the next step.
Get help: Post in our discussion board • Review the GitHub status page
© 2024 GitHub • Code of Conduct • MIT License