Development

How to permanently remove a file from Git history

Advertisements

We all make mistakes sometimes. Pushing files that contain some secrets or sensitive information to a Git repository is fairly common. And even if we revert the commit, it would still be present in the Git history of the project. In such cases, where we want to permanently remove a file from Git history, we need to perform a couple of steps.

1. If the file involved some secrets, revoke them immediately

2. Add the file to gitignore.

Assuming it was a .env file,

echo '.env' >> .gitignore
Bash

3. Permanently remove a file from Git history:

git filter-branch --index-filter "git rm -rf --cached --ignore-unmatch .env" HEAD
Bash

If it is a different file, replace “.env” with the path of the file.

Note: This can be a time-consuming process as it revisits all of the git commits in history and removes the file from there.

4. Force push

git push --force
Bash

Since we rewrote a bunch of commits, we will have to do a force push to modify the git history of the project. If there are multiple branches on the project, or a team working on the project, this might be cumbersome and we would want to search for the commits manually and rebase them instead.

Note: If we only wanted to remove the file and did not care about deleting it from the git history, we would have used the command:

git rm -r --cached .env
Bash

And that is it. Drop-in a comment below if you have any questions.

Saransh Kataria

Born in Delhi, India, Saransh Kataria is the brain behind Wisdom Geek. Currently, Saransh is a software developer at a reputed firm in Austin, and he likes playing with new technologies to explore different possibilities. He holds an engineering degree in Computer Science. He also shares his passion for sharing knowledge as the community lead at Facebook Developer Circle Delhi, NCR which is a developer community in Delhi, India.

View Comments

  • Thanks for this information. One question though, it is still possible to access the URL to the GitHub history where my .env file history 'was' located. I get a warning message from GitHub that "This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository". My repository has always been private and has zero forks, clones, start, etc.

    Is there a way to completely delete this page?

    • Sorry I missed this comment. Are you getting the message after following these steps? Did you do a force push? That should remove the file from git history completely

Share
Published by
Saransh Kataria

Recent Posts

How To Get The Hash of A File In Node.js

While working on a project, I wanted to do an integrity check of a file…

20 hours ago

Native popover API in HTML

Popovers have been a problem that was typically solved by using a third-party solution. But…

1 week ago

Node.js 20.6 adds built-in support for .env files

Node.js 20.6 added built-in support for the .env file. This is an excellent addition to the platform…

2 weeks ago

Object destructuring in TypeScript

Object destructuring is a powerful ES 6 feature that can help developers write cleaner code.…

4 weeks ago

Improve git clone performance in a CI pipeline

Have you felt particularly annoyed by the time it takes to clone a large repository,…

1 month ago

Fix: Hydration failed because the initial UI does not match what was rendered on the server

Within a React or Next.js app, if you encounter the error "Hydration failed because the…

1 month ago
Advertisements