How can I remove a folder that is already deleted from the git history?
In my git repository I have a folder called /foo
(1.2GB of size). I have deleted the folder foo
with rm -rf /foo
beause I do not need it any more. After many other commits I thought. Why is my remote repo so big...I have forgotten to do git rm --cached ...
instead of rm -rf ...
. How can I now remove the folder from git history?
git rm --cached /foo
won't work because the folder is already delete in an earlier commit.
The easiest way to delete a file in your Git repository is to execute the “git rm” command and to specify the file to be deleted. Note that by using the “git rm” command, the file will also be deleted from the filesystem.
Just run the rm command with the -f and -r switch to recursively remove the . git folder and all of the files and folders it contains.
The folder is created when the project is initialized (using git init ) or cloned (using git clone ). It is located at the root of the Git project repository. If we delete the . git folder, the information saved by Git will be lost, and the directory will no longer act as a Git repository.
The documentation covers the similar case of purging a file from history:
git filter-branch --index-filter 'git rm --cached --ignore-unmatch filename' HEAD
Since you are deleting a whole directory, add the -r
flag to git rm
:
git filter-branch --index-filter \
'git rm -r --cached --ignore-unmatch path/to/directory' HEAD
Note that this operation will take several minutes on larger repositories.
More importantly, it will make a new repository with distinct history and checksums. If you previously published your repository, the history of the new one will not be compatible with the history others have pulled.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With