Purging Large Files from a GitLab Repository
Mar 01, 2023
2 minute read
First, disable branch protection on the remote repository so force pushes are allowed: “Settings” -> “Repository” -> scroll down to “Protected branches”.
Clone the project and fetch every branch:
g
c
g
g
g
i
d
i
i
i
t
t
t
t
x
c
x
b
f
p
l
r
e
u
o
#
a
t
l
n
n
c
l
e
e
c
h
n
h
x
t
-
x
e
-
-
a
x
r
r
a
l
l
l
#
t
|
l
h
p
e
g
u
r
l
c
e
l
l
p
s
o
n
t
e
h
d
e
d
m
i
a
r
s
e
t
c
e
t
|
r
o
r
w
b
y
h
r
i
a
l
n
e
c
h
r
e
b
a
y
d
d
r
e
e
f
m
a
o
u
t
l
e
t
;
d
o
g
i
t
b
r
a
n
c
h
-
t
r
a
c
k
"
$
{
r
e
m
o
t
e
#
o
r
i
g
i
n
/
}
"
"
$
r
e
m
o
t
e
"
;
d
o
n
e
Find the large files (list the 10 largest):
1
git rev-list --objects --all | grep " $( git verify-pack -v .git/objects/pack/*.idx | sort -k 3 -n | tail -10 | awk '{print$1}' ) "
Purge the large files you found (one file or directory at a time):
1
git filter-branch --force --index-filter 'git rm -rf --cached --ignore-unmatch path/to/file' --prune-empty --tag-name-filter cat -- --all
Delete and reclaim the space:
1
2
3
4
5
git for -each-ref --format= 'delete %(refname)' refs/original | git update-ref --stdin
rm -rf .git/refs/original/
git reflog expire --expire= now --all
git gc --prune= now
git gc --aggressive --prune= now
Push to the remote repository:
1
2
git push origin --force --all
git remote prune origin
Everyone else can then re-clone the project.
Reference:
https://www.msnao.com/2021/06/15/5031.html