Skip to end of metadata
Go to start of metadata

Maintenance of your Git repository typically involves reducing a repository's size.  If you imported from another version control system, you may need to clean up unnecessary files after the import.  This page focuses on removing large files from a Git repo and contains the following topics:

Be very careful...

The procedure and tools on this page use advanced techniques that involve destructive operations.  Make sure you read carefully and backup your repository before starting. The easiest way to create a backup is to clone your repository using the --mirror flag, and zip the whole clone. With the backup, if you accidentally corrupt a key element of your repo during maintenance, you can recover.

Keep in mind that maintenance can be disruptive to repository users. It is a good idea to communicate repository maintenance with your team or repository followers. Make sure everyone has checked in their code and have agreed to cease development during maintenance.

Understanding file removal from Git history

Recall that cloning a repository clones the entire history — including every version of every source code file.  If a user commits a huge file, such as a JAR, every clone thereafter includes this file. Even if a user ends up removing the file from the project with a subsequent commit, the file still exists in the repository history.  To remove this file from your repository you must:

  • remove the file from your project's current file-tree
  • remove the file from repository history - rewriting Git history, deleting the file from all commits containing it
  • remove all reflog history that refers to the old commit history
  • repack the repository, garbage-collecting the now-unused data using git gc

Git 'gc' (garbage collection) will remove all data from the repository that is not actually used, or in some way referenced, by any of your branches or tags. In order for that to be useful, we need to rewrite all Git repository history that contained the unwanted file, so that it no longer references it - git gc will then be able to discard the now-unused data.

Rewriting repository history is a tricky business, because every commit depends on it's parents, so any small change will change the commit id of every subsequent commit. There are two automated tools for doing this:

  1. the BFG Repo Cleaner - fast, simple, easy to use. Require Java 6 or above.
  2. git filter-branch - powerful, tricky to configure, slow on big repositories. Part of the core Git suite.

Remember, after you rewrite the history, whether you use the BFG or filter-branch, you will need to remove reflog entries that point to old history, and finally run the garbage collector to purge the old data. 

Using the BFG to rewrite history

The BFG is specifically designed for removing unwanted data like big files or passwords from Git repos, so it has a simple flag that will remove any large historical (not-in-your-current-commit) files: '--strip-blobs-bigger-than'

$ java -jar bfg.jar --strip-blobs-bigger-than 100M

Any files over 100MB in size (that aren't in your latest commit - because your latest content is protected by the BFG) will be removed from your Git repository's history. If you'd like to specify files by name, you can do that too:

$ java -jar bfg.jar --delete-files *.mp4

The BFG is 10-1000x faster than git filter-branch, and generally much easier to use - check the full usage instructions and examples for more details.

Alternatively, using git filter-branch to rewrite history

The filter-branch command rewrites a Git repo's revision history, just like the BFG, but the process is slower and more manual. If you don't know where the big file is, your first step will be to find it:

Manually reviewing large files in your repository

Antony Stubbs has written a BASH script that does this very well. The script examines the contents of your packfile and lists out the large files.  Before you begin removing files, do the following to obtain and install this script:

  1. Download the script to your local system.
  2. Put it in a well known location accessible to your Git repository.
  3. Make the script an  executable:

    $ chmod 777 git_find_big.sh
  4. Clone the repository to your local system.
  5. Change directory to your repository root.
  6. Run the Git garbage collector manually.

  7. Find out the size of the .git folder.

    Note this size down for later reference.

  8. List the big files in your repo by running the git_find_big.sh script.

    The big files are all JAR files.  The pack size column is the most relevant.  The aui-dependencies.jar compacts to 169KB  but the emojis.jar compacts only to 580.  The emojis.jar is a candidate for removal.

Running filter-branch

You can pass this command a filter for rewriting the Git index.  For example, a filter can remove a file from every indexed commit.  The syntax for this is the following:

git filter-branch --index-filter 'git rm --cached --ignore-unmatch pathname' commitHASH

The --index-filter option modifies a repo's staging (or index). The --cached option removes a file from the index not the disk.  This is faster as you don't have to checkout each revision before running the filter.  The --ignore-unmatch option in git rm prevents the command from failing if the pathname it is trying to remove isn't there.  By specifying a commit HASH, you remove the pathname from every commit starting with the HASH on up.  To remove from the start, leave this off or you can specify HEAD.  

If all your large files are in different branches, you'll need to delete each file by name. If all the files are within a single branch,  you can delete the branch itself.

Option 1: Delete files by name

Use the following procedure to remove large files:

  1. Run the following command to remove the first large file you identified:

    git filter-branch --index-filter 'git rm --cached --ignore-unmatch filename' HEAD
  2. Repeat Step 1 for each remaining large file.
  3. Update the references in your repository. filter-branch creates backups of your original refs namespaced under refs/original/. Once you're confident that you deleted the correct files, you can run the following command to delete the backed up refs, allowing the large objects to be garbage collected:

    $ git for-each-ref --format="%(refname)" refs/original/ | xargs -n 1 git update-ref -d

Option 2: Delete just the branch

If all your large files are on a single branch,  you can just delete the branch. Deleting the branch automatically removes all the references.

  1. Delete the branch.

    $ git branch -D PROJ567bugfix
  2. Prune all of the reflog references from the branch on back.

    $ git reflog expire --expire=now PROJ567bugfix

Garbage collecting dead data

  1. Prune all of the reflog references from now on back (unless you're explicitly only operating on one branch).

    $ git reflog expire --expire=now --all
  2. Repack the repository by running the garbage collector and pruning old objects.

    $ git gc --prune=now
  3. Push all your changes back to the Bitbucket repository.

    $ git push --all --force
  4. Make sure all your tags are current too:

    $ git push --tags --force

 

 

6 Comments

  1. I followed all steps in Option 1 (except last one - it didn't work), but unfortunately my repository is growing instead of shrinking. It was 60MB, and after "removing" two files it's 90MB now.

    After some googling i found slight better version of git filter-branch (from: http://stackoverflow.com/questions/8740187/git-how-to-remove-file-from-historical-commit)

    git filter-branch --index-filter \ 'git rm --cached --ignore-unmatch path/to/mylarge_50mb_file' \ --tag-name-filter cat -- --all

    Please also change last step in option 1, --tags can't be used together with --all

    Now my repository locally is quite small, but version on bitbucket is still quite huge.

  2. Anonymous

    Option 1: Delete files by name

    step 7 should be:

    git push --tags --force

    not

    git push --all --tags --force
  3. Anonymous

    The option 2 didn't work for me.  `git reflog expire --expire=now my-branch` gives an error: "my-branch points nowhere!"

  4. Option-step 3 doesn't work. I guess the update-ref is malformed.

  5. I'd like to update this page with details about the BFG Repo-Cleaner, a simpler, faster alternative to git-filter-branch for cleansing bad data out of Git repository history. Update: Done

  6. BFG-Repo-Cleaner works pretty fine. The only IMPORTANT tip is to be sure your current state doesn't contain the files you want to remove.

    So first, "git rm" all files to be deleted, commit, push, and then, you can run the tool.