Skip to content

Instantly share code, notes, and snippets.

@filipechagas
Created August 26, 2020 18:32

Revisions

  1. filipechagas created this gist Aug 26, 2020.
    19 changes: 19 additions & 0 deletions git-filter-branch.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,19 @@
    # Git filter-branch

    So today I noticed my git repository was taking too much disk space and discovered it was due to bundler caching gems.
    At first I thought I'd just remove `vendor/cache` directory it be done with it. But that's not how git works, is it?

    That's how I came across `git filter-branch`.

    The following command will remove the `vendor/cache` directory and all its references throughout your repository history.

    `git filter-branch --force --index-filter "git rm --cached --ignore-unmatch -r vendor/cache"`

    ## Undoing it

    And, should you realise you shouldn't have done that, git is also there for you. Whenever you use `filter-branch`, git creates a backup of the previous state for you at `.git/refs/original/refs/heads/NAME_OF_YOUR_BRANCH`.
    Meaning all you have to do is run:

    `git reset --hard refs/original/refs/heads/NAME_OF_YOUR_BRANCH`

    You're welcome ;)