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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

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)

Git vs. GitHub: Version Control and Code Hosting Git vs. GitHub: Version Control and Code Hosting Apr 11, 2025 am 11:33 AM

Git is a version control system, and GitHub is a Git-based code hosting platform. Git is used to manage code versions and supports local operations; GitHub provides online collaboration tools such as Issue tracking and PullRequest.

Is Git the same as GitHub? Is Git the same as GitHub? Apr 08, 2025 am 12:13 AM

Git and GitHub are not the same thing. Git is a version control system, and GitHub is a Git-based code hosting platform. Git is used to manage code versions, and GitHub provides an online collaboration environment.

Is GitHub difficult to learn? Is GitHub difficult to learn? Apr 02, 2025 pm 02:45 PM

GitHub is not difficult to learn. 1) Master the basic knowledge: GitHub is a Git-based version control system that helps track code changes and collaborative development. 2) Understand core functions: Version control records each submission, supporting local work and remote synchronization. 3) Learn how to use: from creating a repository to push commits, to using branches and pull requests. 4) Solve common problems: such as merge conflicts and forgetting to add files. 5) Optimization practice: Use meaningful submission messages, clean up branches, and manage tasks using the project board. Through practice and community communication, GitHub’s learning curve is not steep.

Should I put Git or GitHub on my resume? Should I put Git or GitHub on my resume? Apr 04, 2025 am 12:04 AM

On your resume, you should choose to write Git or GitHub based on your position requirements and personal experience. 1. If the position requires Git skills, highlight Git. 2. If the position values ​​community participation, show GitHub. 3. Make sure to describe the usage experience and project cases in detail and end with a complete sentence.

Does Microsoft own Git or GitHub? Does Microsoft own Git or GitHub? Apr 05, 2025 am 12:20 AM

Microsoft does not own Git, but owns GitHub. 1.Git is a distributed version control system created by Linus Torvaz in 2005. 2. GitHub is an online code hosting platform based on Git. It was founded in 2008 and acquired by Microsoft in 2018.

Should I start with Git or GitHub? Should I start with Git or GitHub? Apr 06, 2025 am 12:09 AM

Starting from Git is more suitable for a deep understanding of version control principles, and starting from GitHub is more suitable for focusing on collaboration and code hosting. 1.Git is a distributed version control system that helps manage code version history. 2. GitHub is an online platform based on Git, providing code hosting and collaboration capabilities.

How to use GitHub for HTML? How to use GitHub for HTML? Apr 07, 2025 am 12:13 AM

The reason for using GitHub to manage HTML projects is that it provides a platform for version control, collaborative development and presentation of works. The specific steps include: 1. Create and initialize the Git repository, 2. Add and submit HTML files, 3. Push to GitHub, 4. Use GitHubPages to deploy web pages, 5. Use GitHubActions to automate building and deployment. In addition, GitHub also supports code review, Issue and PullRequest features to help optimize and collaborate on HTML projects.

What is Git in simple words? What is Git in simple words? Apr 09, 2025 am 12:12 AM

Git is an open source distributed version control system that helps developers track file changes, work together and manage code versions. Its core functions include: 1) record code modifications, 2) fallback to previous versions, 3) collaborative development, and 4) create and manage branches for parallel development.

See all articles