Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Completely remove files from Git repo and remote on GitHub

I accidentally added a folder of images and committed. Then, I made one more commit. Then I removed those files using git rm -f ./images and committed again.

Right now, I have made a lot more commits in that branch (master). In my HEAD, I don't have that ./static/images folder.

Due to this, my repo size has increased a lot. How can I remove those blobs completely? And I want to remove it from my remote GitHub repo too.

like image 497
Abhijeet Rastogi Avatar asked Apr 06 '11 08:04

Abhijeet Rastogi


People also ask

How do I delete files from my GitHub repository?

Browse to the directory in your repository that you want to delete. In the top-right corner, click , then click Delete directory. Review the files you will delete. At the bottom of the page, type a short, meaningful commit message that describes the change you made to the file.

How do I remove all files from a directory in GitHub?

You can now delete an entire directory of files including subdirectories from your web browser: Browse to the directory in the repository and branch that you want to delete. In the top-right corner, click "…", and then Delete directory. Review the list of files.

Can we delete file from Git repository?

The "rm" command helps you to remove files from a Git repository. It allows you to not only delete a file from the repository, but also - if you wish - from the filesystem. Deleting a file from the filesystem can of course easily be done in many other applications, e.g. a text editor, IDE or file browser.


2 Answers

This is what you're looking for: ignoring doesn't remove a file. I suggest you read that page, but here's the specific command to use:

git filter-branch --index-filter \ 'git rm -r --cached --ignore-unmatch <file/dir>' HEAD 

Also, to remove all the deleted files from caches git creates, use:

rm -rf .git/refs/original/ && \ git reflog expire --all && \ git gc --aggressive --prune 

You can find more info about the last command, as well as a script that does everything you want in one single action, here: git: forever remove files or folders from history.

Another links with lots of explanation: Remove sensitive data.

[Edit] Also, see this StackOverflow question: Remove sensitive files and their commits from Git history.

(Commands copied from natacado's answer in the question linked above.) If you have already removed the files from the working copy, the following should work. Find out the hash for the commit that added the unwanted files. Then do:

git filter-branch --index-filter \ 'git update-index --remove filename' <introduction-revision-sha1>..HEAD git push --force --verbose --dry-run git push --force 
like image 135
AVH Avatar answered Oct 10 '22 17:10

AVH


You could just rebase your whole branch and remove both the commit that added the images and the commit that removed them.

git rebase -i master 

You will be presented with a list of commits. Delete the lines that have the rogue commits you want to remove. ("dd" deletes a line in vim, the default editor. Then save with ZZ)

The dangling commits will then be cleaned up in the course of natural git garbage collection, a process you can force with the command given in Darhuuk's answer.

Edit: This will work even if you have pushed to a remote repository, but you'll have to push with --force. (The same applies to the git filter-branch solution).

Note that this will be very annoying to anyone who has pulled from your branch. They should consult "recovering from upstream rebase".


Presumably your original accidental addition of images is part of a commit you wish to keep. In this case, you need to edit the commit during the rebase to split out the parts you wish to keep. You can do this by replacing "pick" in the "rebase -i" list for that commit with "e" (for edit). The rebase process will stop here and you can split the commit with "git commit --amend".

like image 44
RJFalconer Avatar answered Oct 10 '22 18:10

RJFalconer