debian-mirror-gitlab/doc/user/project/repository/reducing_the_repo_size_using_git.md

250 lines
10 KiB
Markdown
Raw Normal View History

2019-10-12 21:52:04 +05:30
---
2020-06-23 00:09:42 +05:30
stage: Create
group: Gitaly
2021-02-22 17:27:13 +05:30
info: To determine the technical writer assigned to the Stage/Group associated with this page, see https://about.gitlab.com/handbook/engineering/ux/technical-writing/#assignments
2019-10-12 21:52:04 +05:30
type: howto
---
2020-06-23 00:09:42 +05:30
# Reduce repository size
2019-01-03 12:48:30 +05:30
2020-06-23 00:09:42 +05:30
Git repositories become larger over time. When large files are added to a Git repository:
2019-02-15 15:39:39 +05:30
2020-06-23 00:09:42 +05:30
- Fetching the repository becomes slower because everyone must download the files.
- They take up a large amount of storage space on the server.
- Git repository storage limits [can be reached](#storage-limits).
2017-09-10 17:25:29 +05:30
2021-10-27 15:23:28 +05:30
Such problems can be detected with [git-sizer](https://github.com/github/git-sizer#getting-started).
2020-06-23 00:09:42 +05:30
Rewriting a repository can remove unwanted history to make the repository smaller.
2020-11-24 15:15:51 +05:30
We **recommend [`git filter-repo`](https://github.com/newren/git-filter-repo/blob/main/README.md)**
over [`git filter-branch`](https://git-scm.com/docs/git-filter-branch) and
[BFG](https://rtyley.github.io/bfg-repo-cleaner/).
2020-06-23 00:09:42 +05:30
2021-02-22 17:27:13 +05:30
WARNING:
2021-01-03 14:25:43 +05:30
Rewriting repository history is a destructive operation. Make sure to back up your repository before
2020-06-23 00:09:42 +05:30
you begin. The best way back up a repository is to
2021-10-27 15:23:28 +05:30
[export the project](../settings/import_export.md#export-a-project-and-its-data).
2019-02-15 15:39:39 +05:30
2020-06-23 00:09:42 +05:30
## Purge files from repository history
2019-02-15 15:39:39 +05:30
2021-03-11 19:13:27 +05:30
To reduce the size of your repository in GitLab, you must first remove references to large files from branches, tags, *and*
2020-10-24 23:57:45 +05:30
other internal references (refs) that are automatically created by GitLab. These refs include:
- `refs/merge-requests/*` for merge requests.
- `refs/pipelines/*` for
2020-11-24 15:15:51 +05:30
[pipelines](../../../ci/troubleshooting.md#fatal-reference-is-not-a-tree-error).
2020-10-24 23:57:45 +05:30
- `refs/environments/*` for environments.
2021-03-11 19:13:27 +05:30
- `refs/keep-around/*` are created as hidden refs to prevent commits referenced in the database from being removed
2020-10-24 23:57:45 +05:30
2021-03-11 19:13:27 +05:30
These refs are not automatically downloaded and hidden refs are not advertised, but we can remove these refs using a project export.
2019-02-15 15:39:39 +05:30
2021-03-11 19:13:27 +05:30
To purge files from a GitLab repository:
2017-09-10 17:25:29 +05:30
2020-07-28 23:09:34 +05:30
1. [Install `git filter-repo`](https://github.com/newren/git-filter-repo/blob/main/INSTALL.md)
2020-06-23 00:09:42 +05:30
using a supported package manager or from source.
1. Generate a fresh [export from the
2021-10-27 15:23:28 +05:30
project](../settings/import_export.html#export-a-project-and-its-data) and download it.
2021-03-11 19:13:27 +05:30
This project export contains a backup copy of your repository *and* refs
we can use to purge files from your repository.
2020-06-23 00:09:42 +05:30
1. Decompress the backup using `tar`:
2017-09-10 17:25:29 +05:30
2020-03-13 15:44:24 +05:30
```shell
2020-06-23 00:09:42 +05:30
tar xzf project-backup.tar.gz
2019-09-30 21:07:59 +05:30
```
2017-09-10 17:25:29 +05:30
2021-02-22 17:27:13 +05:30
This contains a `project.bundle` file, which was created by
2020-06-23 00:09:42 +05:30
[`git bundle`](https://git-scm.com/docs/git-bundle).
2021-03-11 19:13:27 +05:30
1. Clone a fresh copy of the repository from the bundle using `--bare` and `--mirror` options:
2017-09-10 17:25:29 +05:30
2020-03-13 15:44:24 +05:30
```shell
2020-06-23 00:09:42 +05:30
git clone --bare --mirror /path/to/project.bundle
2019-09-30 21:07:59 +05:30
```
2017-09-10 17:25:29 +05:30
2021-11-18 22:05:49 +05:30
1. Navigate to the `project.git` directory:
```shell
cd project.git
```
2020-06-23 00:09:42 +05:30
1. Using `git filter-repo`, purge any files from the history of your repository. Because we are
2021-02-22 17:27:13 +05:30
trying to remove internal refs, we rely on the `commit-map` produced by each run to tell us
2020-06-23 00:09:42 +05:30
which internal refs to remove.
2021-02-22 17:27:13 +05:30
NOTE:
2020-06-23 00:09:42 +05:30
`git filter-repo` creates a new `commit-map` file every run, and overwrite the `commit-map` from
2021-02-22 17:27:13 +05:30
the previous run. You need this file from **every** run. Do the next step every time you run
2020-06-23 00:09:42 +05:30
`git filter-repo`.
2021-03-11 19:13:27 +05:30
To purge all files larger than 10M, the `--strip-blobs-bigger-than` option can be used:
2017-09-10 17:25:29 +05:30
2020-03-13 15:44:24 +05:30
```shell
2020-06-23 00:09:42 +05:30
git filter-repo --strip-blobs-bigger-than 10M
2019-09-30 21:07:59 +05:30
```
2017-09-10 17:25:29 +05:30
2020-06-23 00:09:42 +05:30
To purge specific large files by path, the `--path` and `--invert-paths` options can be combined.
2017-09-10 17:25:29 +05:30
2020-03-13 15:44:24 +05:30
```shell
2020-06-23 00:09:42 +05:30
git filter-repo --path path/to/big/file.m4v --invert-paths
2019-09-30 21:07:59 +05:30
```
2017-09-10 17:25:29 +05:30
2020-06-23 00:09:42 +05:30
See the
[`git filter-repo` documentation](https://htmlpreview.github.io/?https://github.com/newren/git-filter-repo/blob/docs/html/git-filter-repo.html#EXAMPLES)
for more examples and the complete documentation.
2020-10-24 23:57:45 +05:30
1. Because cloning from a bundle file sets the `origin` remote to the local bundle file, delete this `origin` remote, and set it to the URL to your repository:
```shell
git remote remove origin
git remote add origin https://gitlab.example.com/<namespace>/<project_name>.git
```
1. Force push your changes to overwrite all branches on GitLab:
```shell
git push origin --force 'refs/heads/*'
```
2021-02-22 17:27:13 +05:30
[Protected branches](../protected_branches.md) cause this to fail. To proceed, you must
2020-10-24 23:57:45 +05:30
remove branch protection, push, and then re-enable protected branches.
1. To remove large files from tagged releases, force push your changes to all tags on GitLab:
```shell
git push origin --force 'refs/tags/*'
```
2021-02-22 17:27:13 +05:30
[Protected tags](../protected_tags.md) cause this to fail. To proceed, you must remove tag
2020-10-24 23:57:45 +05:30
protection, push, and then re-enable protected tags.
1. To prevent dead links to commits that no longer exist, push the `refs/replace` created by `git filter-repo`.
```shell
git push origin --force 'refs/replace/*'
```
Refer to the Git [`replace`](https://git-scm.com/book/en/v2/Git-Tools-Replace) documentation for information on how this works.
2020-06-23 00:09:42 +05:30
1. Run a [repository cleanup](#repository-cleanup).
## Repository cleanup
> [Introduced](https://gitlab.com/gitlab-org/gitlab-foss/-/issues/19376) in GitLab 11.6.
2021-02-22 17:27:13 +05:30
Repository cleanup allows you to upload a text file of objects and GitLab removes internal Git
2020-06-23 00:09:42 +05:30
references to these objects. You can use
[`git filter-repo`](https://github.com/newren/git-filter-repo) to produce a list of objects (in a
`commit-map` file) that can be used with repository cleanup.
2021-02-22 17:27:13 +05:30
[Introduced](https://gitlab.com/gitlab-org/gitlab/-/merge_requests/45058) in GitLab 13.6,
safely cleaning the repository requires it to be made read-only for the duration
of the operation. This happens automatically, but submitting the cleanup request
fails if any writes are ongoing, so cancel any outstanding `git push`
operations before continuing.
2020-06-23 00:09:42 +05:30
To clean up a repository:
1. Go to the project for the repository.
2020-10-24 23:57:45 +05:30
1. Navigate to **Settings > Repository**.
1. Upload a list of objects. For example, a `commit-map` file created by `git filter-repo` which is located in the
`filter-repo` directory.
2021-04-29 21:17:54 +05:30
If your `commit-map` file is larger than about 250KB or 3000 lines, the file can be split and uploaded piece by piece:
2020-10-24 23:57:45 +05:30
```shell
2021-04-29 21:17:54 +05:30
split -l 3000 filter-repo/commit-map filter-repo/commit-map-
2020-10-24 23:57:45 +05:30
```
2020-06-23 00:09:42 +05:30
1. Click **Start cleanup**.
2021-02-22 17:27:13 +05:30
This:
2020-06-23 00:09:42 +05:30
2021-02-22 17:27:13 +05:30
- Removes any internal Git references to old commits.
- Runs `git gc --prune=30.minutes.ago` against the repository to remove unreferenced objects. Repacking your repository temporarily
causes the size of your repository to increase significantly, because the old pack files are not removed until the
2020-10-24 23:57:45 +05:30
new pack files have been created.
2021-03-11 19:13:27 +05:30
- Unlinks any unused LFS objects attached to your project, freeing up storage space.
2021-02-22 17:27:13 +05:30
- Recalculates the size of your repository on disk.
GitLab sends an email notification with the recalculated repository size after the cleanup has completed.
2020-06-23 00:09:42 +05:30
2021-02-22 17:27:13 +05:30
If the repository size does not decrease, this may be caused by loose objects
being kept around because they were referenced in a Git operation that happened
2021-03-11 19:13:27 +05:30
in the last 30 minutes. Try re-running these steps after the repository has been
2021-02-22 17:27:13 +05:30
dormant for at least 30 minutes.
2020-06-23 00:09:42 +05:30
When using repository cleanup, note:
2020-07-28 23:09:34 +05:30
- Project statistics are cached. You may need to wait 5-10 minutes to see a reduction in storage utilization.
2021-02-22 17:27:13 +05:30
- The cleanup prunes loose objects older than 30 minutes. This means objects added or referenced in the last 30 minutes
are not be removed immediately. If you have access to the
[Gitaly](../../../administration/gitaly/index.md) server, you may slip that delay and run `git gc --prune=now` to
2020-06-23 00:09:42 +05:30
prune all loose objects immediately.
2021-02-22 17:27:13 +05:30
- This process removes some copies of the rewritten commits from the GitLab cache and database,
2020-06-23 00:09:42 +05:30
but there are still numerous gaps in coverage and some of the copies may persist indefinitely.
[Clearing the instance cache](../../../administration/raketasks/maintenance.md#clear-redis-cache)
may help to remove some of them, but it should not be depended on for security purposes!
## Storage limits
Repository size limits:
2020-11-24 15:15:51 +05:30
- Can [be set by an administrator](../../admin_area/settings/account_and_limit_settings.md#account-and-limit-settings)
2021-03-11 19:13:27 +05:30
on self-managed instances. **(PREMIUM SELF)**
2020-11-24 15:15:51 +05:30
- Are [set for GitLab.com](../../gitlab_com/index.md#account-and-limit-settings).
2020-06-23 00:09:42 +05:30
When a project has reached its size limit, you cannot:
- Push to the project.
- Create a new merge request.
- Merge existing merge requests.
- Upload LFS objects.
You can still:
- Create new issues.
- Clone the project.
2021-01-03 14:25:43 +05:30
If you exceed the repository size limit, you can:
2020-06-23 00:09:42 +05:30
1. Remove some data.
1. Make a new commit.
1. Push back to the repository.
2021-01-03 14:25:43 +05:30
If these actions are insufficient, you can also:
2020-06-23 00:09:42 +05:30
- Move some blobs to LFS.
- Remove some old dependency updates from history.
2021-01-03 14:25:43 +05:30
Unfortunately, this workflow doesn't work. Deleting files in a commit doesn't actually reduce the
size of the repository, because the earlier commits and blobs still exist. Instead, you must rewrite
history. We recommend the open-source community-maintained tool
2020-06-23 00:09:42 +05:30
[`git filter-repo`](https://github.com/newren/git-filter-repo).
2021-02-22 17:27:13 +05:30
NOTE:
Until `git gc` runs on the GitLab side, the "removed" commits and blobs still exist. You also
2020-06-23 00:09:42 +05:30
must be able to push the rewritten history to GitLab, which may be impossible if you've already
exceeded the maximum size limit.
In order to lift these restrictions, the administrator of the self-managed GitLab instance must
increase the limit on the particular project that exceeded it. Therefore, it's always better to
proactively stay underneath the limit. If you hit the limit, and can't have it temporarily
increased, your only option is to:
1. Prune all the unneeded stuff locally.
1. Create a new project on GitLab and start using that instead.
2021-02-22 17:27:13 +05:30
WARNING:
2020-06-23 00:09:42 +05:30
This process is not suitable for removing sensitive data like password or keys from your repository.
2021-02-22 17:27:13 +05:30
Information about commits, including file content, is cached in the database, and remain
2020-06-23 00:09:42 +05:30
visible even after they have been removed from the repository.
2019-10-12 21:52:04 +05:30
2020-11-24 15:15:51 +05:30
## Troubleshooting
2019-10-12 21:52:04 +05:30
2020-11-24 15:15:51 +05:30
### Incorrect repository statistics shown in the GUI
2019-10-12 21:52:04 +05:30
2020-11-24 15:15:51 +05:30
If the displayed size or commit number is different from the exported `.tar.gz` or local repository,
you can ask a GitLab administrator to [force an update](../../../administration/troubleshooting/gitlab_rails_cheat_sheet.md#incorrect-repository-statistics-shown-in-the-gui).