70

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?

sth
  • 222,467
  • 53
  • 283
  • 367
mrblah
  • 99,669
  • 140
  • 310
  • 420

5 Answers5

135

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.

Aswin Kumar
  • 5,158
  • 5
  • 34
  • 39
Alan Haggai Alavi
  • 72,802
  • 19
  • 102
  • 127
  • 5
    thanks, that really works! You can also add: git push --all --force – Vagner do Carmo Mar 05 '15 at 01:50
  • 4
    Thanks. It took me a few tries because I did not provide the full relative path to the file (from the root), and just had the filename. – mdiehl13 Aug 11 '15 at 11:17
  • 4
    Remember to always warn all your peers when you're about to do a history rewrite on a remote repository (`git push --all --force`), or else they will face all kinds of problems when performing a pull. – pedromanoel Sep 09 '15 at 13:52
  • 1
    If this happens to you, you must perform a reset on your affected branches. First `git fetch` then `git status`. If you see that the branches have diverged, stash your changes and `git reset --hard origin/`. – pedromanoel Sep 09 '15 at 13:54
  • This didn't work, and corrupted git. All future commits now fail. – Adam Aug 03 '17 at 20:30
  • 1
    Adam: This does not *corrupt* Git. Commits will not fail. The first push has to be forced. Did you read the linked article? – Alan Haggai Alavi Aug 03 '17 at 23:38
  • life Saver. you really should add git push --all --force. – Jean Raymond Daher Oct 23 '21 at 16:58
  • Be warned, this reverted a bunch of my files and even moved files back to an old directory. Fortunately, I hadn't made that many changes. In the end, I ended up just deleting the entire repo since I hadn't done that much to it. Git really needs to make it easier to just go into the site and remove a single historical version. – garek007 Dec 29 '22 at 21:10
48

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.

Andrzej Jozwik
  • 14,331
  • 3
  • 59
  • 68
  • 2
    This answer was super helpful and worked like a charm for me. I needed to remove a large folder which had accidentally been committed a few months ago, bloating our repo by hundreds of megs. The process took about an hour to complete.I did have an issue with the "git push --all --force". Since these changes were committed to our develop branch, I first had to do a "git checkout develop", then perform the "git push" to get the changes to sync up to the remote repo. – Sage Jun 29 '18 at 14:13
  • I try out this procedure and see, that the files are not removed in the remote repository. But they will not checked out when cloning from remote repository. Executing the 4 commands (`rm -rf ...` ... `git gc --aggressive --prune=now`) in the remote repository remove the files in remote repository too. But you need write-access to the remote repository folder. – Manfred Steiner Dec 11 '20 at 10:46
7

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/

dezhi
  • 809
  • 8
  • 10
2

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

jamessan
  • 41,569
  • 8
  • 85
  • 85
  • 1
    Link is broken (cannot be found). I believe this is the [same](https://git-scm.com/docs/git-filter-branch). – haxpor Dec 05 '20 at 13:16
0

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.

Saket Milan
  • 103
  • 1
  • 8