13th February 2009
Once I accidentally added circa 300 MiB of archive files to one of my git repositories (which was as small as 5 MiB). I removed those files as soon as I noticed them, but the .git directory still preserved commits with those files, and still occupied over 300 MiB.
This method worked for me, but I couldn’t push my rebased repository to the gitosis. I would need to re-init the gitosis repository from my rebased, but I’m not yet prepared to do that.
There is also a slightly different method (which relies on a temporary tag instead of a temporary branch), documented in Git online manual pages; I prefer the temporary branch method.
Below is a full copy-paste of the winning answer by Charles Bailey:
# create and check out a temporary branch at the location of the bad merge
git checkout -b tmpfix
# remove the incorrectly added file
git rm somefile.orig
# commit the amended merge
git commit –amend
# go back to the master branch
git checkout master
# replant the master branch onto the corrected merge
git rebase tmpfix
# delete the temporary branch
git branch -d tmpfix
Also, in my case this thread at stackoverflow was highly useful. I start enjoying the concise and compact style of Charles Bailey :
# detach head and move to D commit
# move HEAD to A, but leave the index and working tree as for D
git reset –soft
# Redo the D commit re-using the commit message, but now on top of A
git commit -C
# Re-apply everything from the old D onwards onto this new place
git rebase –onto HEAD
Note: this example is for a branch of commits R–A–B–C–D–E–HEAD, where commits B & C should be removed from commit history.
Another option is to use git rebase –interactive (for the same example):
git rebase -i HEAD~5
Unfortunately, rebasing on a public (pushed out) branch has no effect for other users of the current gitosis repository.
An alternative to re-initializing gitosis repository from rebased scratch could be switching to a different (rebased) branch, and deleting the outdated master branch; but I haven’t investigated if this works.