Firstly, I should say that if there are only a few commits containing sensitive information, you'd be better to use git rebase -i
to remove them. But that can get complicated when those files have been touched frequently. Also, it's very hard to get rid of the initial commit using git-rebase
, so if your initial commit contains the things you want to delete, then things get harder.
But anyway, all of the answers above to fold the entire repo into one commit seem overly complicated, and will also be quite slow involving lots of intermediate directories and possibly trashing all the other configuration stored in .git
.
A far quicker approach is to create an orphaned commit that exactly matches an existing commit, for example:
$ TREE=`git cat-file -p master |sed '1,/^$/s/^tree //p;d;'`
$ COMMIT=`echo Truncated tree | git commit-tree $TREE`
$ git branch truncated-master $COMMIT
Replace master
and truncated-master
as appropriate, and change the commit message as you see fit. If you're feeling really brave, go ahead and:
$ git branch backup-master-just-in-case-i-regret-it-later master
$ git push -f origin truncated-master:master
But, I would really avoid doing this last step if I were you, as it will cause problems for every other person currently using the repo. Instead, just switch to using this new branch (maybe with a better name) and don't publicise the old one.