Autarchy of the Private Cave

Tiny bits of bioinformatics, [web-]programming etc

    • Archives

    • Recent comments

    Git: how to remove file and commit from history

    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.

    I have found the solution at stackoverflow (see also this question).

    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
    git checkout

    # 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 master

    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.


    2 Responses to “Git: how to remove file and commit from history”

    1. Dateien von einem Git Repository löschen Says:

      [...] Git geht sogar einen Schritt weiter und ermöglicht unter bestimmten Voraussetzungen Dateien komplett vom repository zu löschen. Also so, dass sie auch in der History nicht mehr auftauchen. Nähere Informationen dazu gibt es hier: Git: how to remove file and commit from history [...]

    2. How to truncate git history (sample script included) Says:

      [...] Git: how to remove file and commit from history [...]

    Leave a Reply

    XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>