Git is a popular version control system that allows you to work with multiple files and directories. Once you start tracking a file in your git repository, it will track all changes in it and include its changes in your repository’s history, even without you explicitly specifying it. But sometimes you may need to remove one or more files from your repository. In this article, we will learn how to remove file from git history.
How to Remove File from Git History
Please note, the following method may generate a lot of commits and divergence so be careful before you do this.
You can easily remove file from git history with the following command. Replace path_to_file with the file path which you want to delete.
$ git filter-branch --index-filter 'git rm -rf --cached --ignore-unmatch path_to_file' HEAD
Once you have verified the changes and are satisfied with it, push the changes using the following command.
git push origin --force --all
If you have only recently added the file, it is advisable to revert the last few changes and rebase your git repository.
In this article, we have learnt how to remove file from git history.
Be careful before you run the above commands, since it can cause a lot of commits in the process.
Also read:
How to Get One File from Another Branch in Git
How to Change Git Commit Message
How to Force Git Pull to Overwrite Local Files
How to Delete Git Branch Locally & Remotely
How to Reset or Revert File to Specific Commit in Git
Sreeram has more than 10 years of experience in web development, Python, Linux, SQL and database programming.