78

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

I don't want this file to be versioned. It was just a one-time thing really.

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

1

5 Answers 5

142

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.

Sign up to request clarification or add additional context in comments.

8 Comments

thanks, that really works! You can also add: git push --all --force
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.
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.
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/<branch>.
Adam: This does not corrupt Git. Commits will not fail. The first push has to be forced. Did you read the linked article?
|
56

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.

2 Comments

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.
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.
9

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 Removing sensitive data from a repository.

Comments

2

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

1 Comment

Link is broken (cannot be found). I believe this is the same.
0

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

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

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

Then download latest version of the BFG JAR file from BFG Repo-Cleaner. Rename it as bfg.jar, and paste it inside the YourRepoName.git folder.

Then run following lines in Git Bash.

java -jar bfg.jar --delete-files yourfilename (only the file name is needed; there isn't any need to mention the 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 the above command)

I faced an issue here. My project had 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 the master branch.

git push -u origin master

It’s 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 repository size from 40 MB to 4 MB by removing unwanted APK files which got pushed with my commits.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.