DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

Gerbrand van Dieyen
Gerbrand van Dieyen

Posted on • Originally published at software-creation.nl on

Purging files from Git history

Most developers, like me, use Git for versioning. Even for personal projects, it’s great to have a history of files available. Deleting a file in git will cause it to remove from your working directory, while the file is still present in the history. Usually this is what you want, but sometimes you might want to remove a file completely including it’s history.

There’s a command for that but typically for git it’s not exactly intuitive. To alter history, you can use the filter-branch command. Filter branch can be supplied with a filter, which is a shell command that’s executed on each commit.

  • To purge a file from history, as if it never existed execute:

git filter-branch -f --index-filter 'git rm --cached --ignore-unmatch MYFILE public' HEAD

Where you can replace MYFILE with one or more files you want to delete.

  • If you want to purge a whole directory, including it’s content, add the -r switch:

git filter-branch -f --index-filter 'git rm --cached --ignore-unmatch MYDIRECTORY public' HEAD

Great if you checked in a large file, or file in a public repository that shouldn’t be public. For more information, read the git manual or the numerous tutorials that exist. For example, git-filter can also be used to move a file from one repository to another while preserving history.

Top comments (0)

Head to your account's Settings to...

🌚 Enable dark mode
πŸ”  Change your default font
πŸ“š Adjust your experience level to see more relevant content