How to permanently delete a file stored in GIT?

Git

Git Problem Overview


I backed up my database to GIT just so I could get the db at my home computer.

I don't want this file to be versioned, it was just a 1 time thing really.

Can I delete it for good so GIT doesn't keep track of it going forward or historically?

Git Solutions


Solution 1 - Git

I always find Guides: Completely remove a file from all revisions feed helpful.

> To remove the file called Rakefile: > > git filter-branch --force --index-filter
>   'git rm --cached --ignore-unmatch Rakefile'
>   --prune-empty --tag-name-filter cat -- --all This command will run the entire history of every branch and tag, changing any commit > that involved the file Rakefile, and any commits afterwards. Commits > that are empty afterwards (because they only changed the Rakefile) are > removed entirely.

Solution 2 - Git

Update for remote repository:

git filter-branch -f --index-filter "git rm -rf --cached --ignore-unmatch FOLDERNAME" -- --all

replace FOLDERNAME with the file or folder you wish to remove from the given git repository.

rm -rf .git/refs/original/

git reflog expire --expire=now --all

git gc --prune=now

git gc --aggressive --prune=now

Now push all the changes to the remote repository

git push --all --force

This would clean up the remote repository.

Solution 3 - Git

You can also use bfg for ease.

>The BFG is a simpler, faster alternative to git-filter-branch for cleansing bad data out of your Git repository history:

Removing Crazy Big Files Removing Passwords, Credentials & other Private data

$ bfg --delete-files YOUR-FILE-WITH-SENSITIVE-DATA

Or just replace all the occurrences of some file:

$ bfg --replace-text passwords.txt

check https://rtyley.github.io/bfg-repo-cleaner/ and https://help.github.com/articles/removing-sensitive-data-from-a-repository/

Solution 4 - Git

You can with git filter-branch's --index-filter.

Solution 5 - Git

I would like to share most simplest and easy to understand solution which worked for me.

First clone a fresh copy of your repo, using the --mirror flag:

git clone --mirror https://github.com/username/youproject.git

Then Download latest version of BFG jar file from https://rtyley.github.io/bfg-repo-cleaner/ rename it as bfg.jar and paste it inside YourRepoName.git folder.

Then run following lines in git bash.

java -jar bfg.jar --delete-files yourfilename (only file name is needed, no need to mention path)

git reflog expire --expire=now --all && git gc --prune=now --aggressive (it will strip out the unwanted dirty data which has been expelled out due to above command)

I faced issue here. My project was having open pull requests. If there are any open pull requests then you need to run this command

git show-ref | cut -d' ' -f2 | grep 'pull-request' | xargs -r -L1 git update-ref -d

After this you can simply push master branch.

git push -u origin master

Its done. But remember to keep a copy in local of old repository before performing above action. All unmerged branches and open pull requests may get deleted.

I reduced my repo size from 40mb to 4mb by removing unwanted apk files which got pushed with my commits.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionmrblahView Question on Stackoverflow
Solution 1 - GitAlan Haggai AlaviView Answer on Stackoverflow
Solution 2 - GitAndrzej JozwikView Answer on Stackoverflow
Solution 3 - GitdezhiView Answer on Stackoverflow
Solution 4 - GitjamessanView Answer on Stackoverflow
Solution 5 - GitSaket MilanView Answer on Stackoverflow