1

I have committed a file that shouldn't be in a git repository. I have deleted it and committed the deletion, but I can still access the file via the git history.

How do I delete a committed file permanently, so that there is no trace of it?

Roberto Tyley
  • 24,513
  • 11
  • 72
  • 101
Paul Verest
  • 60,022
  • 51
  • 208
  • 332
  • Does "for always" mean "permanently"? –  May 15 '13 at 07:06
  • And what do you mean with `no trace`? I don't think it is possible to remove the file from the history, it will be present in previous commits. But that is how version control works – ThanksForAllTheFish May 15 '13 at 07:09
  • check out Github's [remove sensitive data](https://help.github.com/articles/remove-sensitive-data) page – fbstj May 15 '13 at 07:09
  • possible duplicate of [How to permanently delete a file stored in GIT?](http://stackoverflow.com/questions/2004024/how-to-permanently-delete-a-file-stored-in-git) – AD7six May 15 '13 at 07:10
  • http://stackoverflow.com/a/2158271/1615903 – 1615903 May 15 '13 at 07:11
  • @mardavi That was fgb edit. All and cexbrayat Thank so much for answer. – Paul Verest May 15 '13 at 08:00
  • possible duplicate of [Completely remove unwanted file from Git repository history](http://stackoverflow.com/questions/307828/completely-remove-unwanted-file-from-git-repository-history) – Andy Lester May 15 '13 at 16:26

2 Answers2

8

You can remove permanently a file using filter-branch. It will apply the command in argument in every revision of your repository.

git filter-branch --tree-filter 'rm -rf your_file' HEAD

This detailed post might help you. The git doc is pretty helpful too.

As mentioned in the comments, you will also need a git gc to clean your local commits and then git pushto update your remote repository.

cexbrayat
  • 17,772
  • 4
  • 27
  • 22
2

This might help:

https://help.github.com/articles/remove-sensitive-data

The following command will do this:

git clone https://github.com/defunkt/github-gem.git
# Initialized empty Git repository in /Users/tekkub/tmp/github-gem/.git/
# remote: Counting objects: 1301, done.
# remote: Compressing objects: 100% (769/769), done.
# remote: Total 1301 (delta 724), reused 910 (delta 522)
# Receiving objects: 100% (1301/1301), 164.39 KiB, done.
# Resolving deltas: 100% (724/724), done.

cd github-gem

git filter-branch --force --index-filter \
  'git rm --cached --ignore-unmatch Rakefile' \
  --prune-empty --tag-name-filter cat -- --all
# Rewrite 48dc599c80e20527ed902928085e7861e6b3cbe6 (266/266)
# Ref 'refs/heads/master' was rewritten

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. Note that you'll need to specify the full path of the file you want to remove, not just its filename.

Kai
  • 1,953
  • 2
  • 13
  • 18
  • You are missing a `git gc` - and there's no mention that this won't affect the remote repo (if there is one). – AD7six May 15 '13 at 07:13
  • @AD7six Follow the given link in my post, there is then the `git gc` command. The posted code is only a short introduction, it is highly recommended to read the whole article. – Kai May 15 '13 at 07:19
  • your _answer_ is incomplete. It's highly recommended to write answers such that references are supporting - not fundamental - information. I'm quite familiar with the reference though :). – AD7six May 15 '13 at 13:40