Home Development Tools git How to delete a large Git folder in GitLab

How to delete a large Git folder in GitLab

Apr 26, 2023 am 09:16 AM

在使用GitLab进行版本管理的过程中,我们可能会遇到一个问题:Git库中存储了太多大文件,导致GitLab的储存空间不足。此时,我们就需要删除Git中的Git大文件夹来腾出一些空间。但是,由于Git的特殊性质,删除文件夹不是和其他文件一样删除,而是需要一些额外操作。本文将介绍如何在GitLab上删除Git大文件夹的方法。

1、确认Git大文件夹

首先,我们需要确认在GitLab上库中的哪个文件夹占用了最多的空间,进而决定哪个文件夹是Git大文件夹。我们可以通过以下命令来确定:

git rev-list --objects --all | \
  git cat-file --batch-check='%(objecttype) %(objectname) %(objectsize) %(rest)' | \
  sed -n 's/^blob //p' | \
  sort --numeric-sort --key=2 | \
  cut -c 1-12,41- |
  # 此时应该得到一个类似如下的输出
  # 6fb171 9336398 vendor/assets/bower_components/bootstrap/dist/js/bootstrap.min.js
  # 8cf6f9 9430549 vendor/assets/bower_components/jquery/jquery.js
  # 45babf 11219868 vendor/assets/bower_components/angular/angular.js
  # …
  awk '$2 >= 1000000 { print }'
Copy after login

此命令的作用是列举出当前库中所有所有提交中的 blob 对象,并按照 size 从小到大排序。最后的 awk '$2 >= 1000000 { print }' 表示只输出文件大小大于等于 1000000 字节的文件。这个命令中的数字可以自行更改成你所需要的大小。

2、从GitLab数据库中删除Git大文件夹

在确认了Git大文件夹之后,我们可以通过以下命令从GitLab的数据库中删除该文件夹:

sudo -u gitlab-psql /opt/gitlab/embedded/bin/psql -d gitlabhq_production
Copy after login

此命令会进入GitLab的数据库中。在这里,我们需要注意以下几点:

  • 在执行这条命令之前,请确保你已经备份了整个gitlabhq_production数据库;
  • 进入数据库后,你需要输入明确的SQL指令,请谨慎操作;
  • 请确认你所登录的用户有权限进行数据库的操作。

在进入数据库之后,我们可以通过以下命令进入出我们所需要删除的文件夹所在的项目:

\x
SELECT * FROM projects;
Copy after login

此命令会列举出数据库中所包含的所有项目,我们可以用这个命令找到我们需要删除的Git大文件夹所在的项目。

接下来,我们需要根据项目的ID,进入该项目的repositories表中。

SELECT * FROM repositories WHERE project_id = <project_id>;
Copy after login

此命令会在repositories表中找到在指定项目中的repository记录。我们需要记下这个记录的ID,以便后续操作。

接下来,我们需要进入该repository记录所对应的repository_storages表中。

SELECT * FROM repository_storages WHERE repository_id = <repository_id>;
Copy after login

在上述命令中,指代我们刚刚记录下的repository记录的ID。

现在,我们需要确定我们需要删除的大文件夹所在的存储路径。如果你不确定这个路径是什么,可以打开GitLab的页面,到存储空间管理页面,找到对应项目的存储路径。

最后,我们就可以通过以下命令从数据库中删除指定的大文件夹了:

DELETE FROM repository_uploads WHERE path LIKE '<path>/%';
Copy after login

在上述命令中,指代我们刚刚确定的大文件夹所在的存储路径。这个命令会将该路径下的所有文件都删除掉。

3、删除服务器上的Git大文件夹

在数据库中删除Git大文件夹后,我们还需要在服务器上将这些文件从磁盘上彻底删除。你可以直接通过rm命令删除大文件夹,例如:

sudo rm -rf <path>
Copy after login

在上述命令中,指代我们刚刚确定的大文件夹所在的存储路径。但是,如果你在执行该命令时遇到问题,请先停止GitLab和Nginx的服务。在删除文件夹后,我们还需要重新启动这两个服务。

完毕!你已经成功地在GitLab上删除了Git大文件夹。

总结

本文介绍了如何在GitLab上删除Git大文件夹。要删除文件夹主要需要进行两个步骤:从数据库中删除文件夹记录,然后从磁盘上彻底删除文件夹。删掉Git大文件夹之后,我们可以为GitLab腾出更多的空间来存储其他文件。

The above is the detailed content of How to delete a large Git folder in GitLab. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to use git management tools for complete usage of git management tools How to use git management tools for complete usage of git management tools Mar 06, 2025 pm 01:32 PM

This article provides a guide to Git management, covering GUI tools (Sourcetree, GitKraken, etc.), essential commands (git init, git clone, git add, git commit, etc.), branch management best practices (feature branches, pull requests), and merge con

How to push the specified commit How to push the specified commit Mar 06, 2025 pm 01:39 PM

This guide explains how to push a single Git commit to a remote branch. It details using a temporary branch to isolate the commit, pushing this branch to the remote, and then optionally deleting the temporary branch. This method avoids conflicts and

How to view commit contents How to view commit contents Mar 06, 2025 pm 01:41 PM

This article details methods for viewing Git commit content. It focuses on using git show to display commit messages, author info, and changes (diffs), git log -p for multiple commits' diffs, and cautions against directly checking out commits. Alt

The difference between commit and push of git The difference between commit and push of git Mar 06, 2025 pm 01:37 PM

This article explains the difference between Git's commit and push commands. git commit saves changes locally, while git push uploads these committed changes to a remote repository. The article highlights the importance of understanding this distin

How to solve the failure of git commit submission How to solve the failure of git commit submission Mar 06, 2025 pm 01:38 PM

This article addresses common Git commit failures. It details troubleshooting steps for issues like untracked files, unstaged changes, merge conflicts, and pre-commit hooks. Solutions and preventative measures are provided to ensure smoother Git wo

The difference between add and commit of git The difference between add and commit of git Mar 06, 2025 pm 01:35 PM

This article explains the distinct roles of git add and git commit in Git. git add stages changes, preparing them for inclusion in the next commit, while git commit saves the staged changes to the repository's history. This two-step process enables

How to use git management tools Tutorial for using git management tools for beginners How to use git management tools Tutorial for using git management tools for beginners Mar 06, 2025 pm 01:33 PM

This beginner's guide introduces Git, a version control system. It covers basic commands (init, add, commit, status, log, branch, checkout, merge, push, pull) and resolving merge conflicts. Best practices for efficient Git use, including clear comm

What is git code management tool? What is git code management tool? What is git code management tool? What is git code management tool? Mar 06, 2025 pm 01:31 PM

This article introduces Git, a distributed version control system. It highlights Git's advantages over centralized systems, such as offline capabilities and efficient branching/merging for enhanced collaboration. The article also details learning r

See all articles