1

Assume, at the 1 February we have Git repository with size 5 MB. After adding some images and committing them to 10 February we receive repo with 15 MB.

How to remove this images from repo and decrease size to 5 MB?

A. Innokentiev
  • 681
  • 2
  • 11
  • 27

3 Answers3

2

The easier to clean a git repository is to use 'BFG repo cleaner'

https://rtyley.github.io/bfg-repo-cleaner/

(easier than git-filter-branch and a lot faster!)

But be aware that it will change all the history, modifying sha1 of commits and impacting all the developers!

Mus
  • 7,290
  • 24
  • 86
  • 130
Philippe
  • 28,207
  • 6
  • 54
  • 78
0

Description

The two instructions below will go through each commit in your repository and delete the file you've specified in <file>.<extension>

Note: Be careful since this does change the history of your repository!

Example

git filter-branch --tree-filter "rm -f <file>.<extension>" -- --all

or

git filter-branch --index-filter "git rm --cached --ignore-unmatch <file>.<extension>" -- --all

Note: Difference between the two filters (--tree-filter and --index-filter) is the place where everything happens. In later, the instruction is carried out in staging area as oppose to working directory. Also, after you run filter-branch, git leaves a backup of your tree in the .git directory. By default, you can't run filter-branch again because it won't overwrite the backup. In order to bypass this you can add -f option after filter-branch instruction. This will overwrite backup.

Reference

git-filter-branch

e.doroskevic
  • 2,129
  • 18
  • 25
-3

cd into your repository and the directory for your files

Example:

cd app/public/images

then enter the following command;

git rm -rf ./*

This will delete the files in your directory from the git repository. To break it down, with git' you're obviously directing git to do something. 'rm' is the command to remove files. the flags '-rf' does two things. The 'r' is to allow recursive removal; and the 'f' is to force action by overriding the up-to-date check.

The './*' selects all the files in the directory you are in when you enter the command.

You can find more info here: https://git-scm.com/docs/git-rm

William Carron
  • 410
  • 7
  • 17