Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove a deleted folder from git history [duplicate]

Tags:

git

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.

like image 768
Marten Bauer Avatar asked Jan 20 '14 09:01

Marten Bauer


People also ask

How do I completely delete a file from git history?

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.

How do I permanently delete a directory in git?

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.

What happens if I delete git folder?

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.


1 Answers

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.

like image 188
user4815162342 Avatar answered Sep 19 '22 20:09

user4815162342