Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing commits that have a size (in disk space) larger than a given value

Tags:

git

After reading through:

How to remove a too large file in a commit when my branch is ahead of master by 5 commits

https://help.github.com/en/articles/working-with-large-files

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

https://help.github.com/en/articles/removing-sensitive-data-from-a-repository

Show commit size in git log

Git - get all commits and blobs they created

I couldn't find an elegant solution of removing commits that exceed a given size (on disk). These commits do not necessarily have large files, but are large in and of themselves (have many ~200 KB dependencies).

How can such commits be removed from the repository?

like image 312
Sebi Avatar asked Jan 16 '26 23:01

Sebi


1 Answers

First a note :

git compresses files when it stores them in its .git/ structure, and tries to store similar files using only their diffs ;

in that sense, it is difficult to spot "what commit uses up the most space in my .git/ folder".


If you want to measure how much space the files in a commit take up when checked out :

git ls-tree -r -l <commitid>

will list the files along with their individual sizes

git ls-tree -r -l <commitid> | awk '{ sum += $4 } END { print sum }'

will print the total size of these files.


You can put the above shortcut in a script and see what commits take up more than xx bytes, the next thing is : can you get rid of said commits ?

You may tell git to delete the end of a branch :

If all 'B's mark 'big commits' :

               +-- create a new branch here
               v
*--*--*--*--*--*--B--B--B--B <- branchA
    \              \
     \              \-B--B <- branchB
      \
       *--*--*--* <- branchC
              \
               \--B <- branchD

In the above diagram, you can tell git to forget branchA, branchB and branchD (and possibly create a new reference to keep the first "no so big" commits),

but when a commit appears in the middle of a branch :

*--*--B--B--*--* <- branchE

your notion of "delete the two Bs" depends heavily on what is stored in your git repo and how you can remove these commits from a branch's history.

The general advice is : do not delete commits.

like image 148
LeGEC Avatar answered Jan 19 '26 17:01

LeGEC



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!