简体   繁体   English

如何压缩本地git repo

[英]How to compact local git repo

Can I achieve the result of git clone --depth=1 on a local already cloned repo which has all history while the aim is to keep all the ignored files in place? 我能否在具有所有历史记录的本地已克隆存储库上获得git clone --depth=1的结果,而目的是将所有被忽略的文件保留在原处?

I have several git projects consuming lots of space and I would like to remove all git history to release some space. 我有几个git项目占用大量空间,我想删除所有git历史记录以释放一些空间。 Thanks. 谢谢。

UPDATE UPDATE

To be more precise: 更准确地说:

  • I don't want to do any manual work with moving files here and there, deleting repo, recreate, etc 我不想在此处移动文件,删除存储库,重新创建等任何手动操作
  • I have some config files in assume-unchanged state which must be kept 我有一些配置文件处于assume-unchanged状态,必须保留
  • I have many gitignored files which must be left in place 我有很多必须忽略的文件

These reasons can make it clear why I don't want to re-clone and set up all credentials and stuff on 30 repos. 这些原因可以清楚说明为什么我不想重新克隆并在30个存储库上设置所有凭据和内容。

Ideally I'm looking for something I could even run in batch on all project. 理想情况下,我正在寻找可以在所有项目中批量运行的东西。 If there's no such thing, then I'm fine with it of course. 如果没有这种东西,那我当然会满意的。

I have several git projects consuming lots of space and I would like to remove all git history to release some space. 我有几个git项目占用大量空间,我想删除所有git历史记录以释放一些空间。 Thanks 谢谢

Why not simply delete the projects and re-clone it again with the git clone --depth --branch ... to clone a specific branch and the latest commit as you did. 为什么不简单地删除项目,然后使用git clone --depth --branch ...再次将其git clone --depth --branch ...以克隆特定的分支和最新的提交。


How to pack your repository: 如何打包您的存储库:

You have to run gc to do it. 您必须运行gc来执行此操作。

# Expire all the unreachable content and pack the repo in the most 
# compact way.
git gc --aggressive --prune=now

# also clear the reflog as well
git reflog expire --expire=now --all

Clean out large files 清理大文件

You should use this tool to clean your repository history: 您应该使用此工具来清理存储库历史记录:
https://rtyley.github.io/bfg-repo-cleaner/ https://rtyley.github.io/bfg-repo-cleaner/

It the prefect tool for this kind of task 它是完成此类任务的完美工具

BFG Repo-Cleaner BFG回购清洁剂

an alternative to git-filter-branch. git-filter-branch的替代方法。

The BFG is a simpler, faster alternative to git-filter-branch for cleansing bad data out of your Git repository history: BFG是git-filter-branch的一种更简单,更快的替代方法,用于从Git存储库历史记录中清除不良数据

  • Removing Crazy Big Files 删除疯狂的大文件
  • Removing Passwords, Credentials & other Private data 删除密码,凭据和其他私人数据

Examples (from the official site) 示例(来自官方网站)

In all these examples bfg is an alias for java -jar bfg.jar. 在所有这些示例中,bfg是java -jar bfg.jar的别名。

# Delete all files named 'id_rsa' or 'id_dsa' :
bfg --delete-files id_{dsa,rsa}  my-repo.git

在此处输入图片说明


After you have cleaned your repository use this tool to store your large files. 清理存储库后,请使用此工具存储大文件。

在此处输入图片说明

What I would do is make a new folder, and clone the repo from the existing folder into it: 我要做的是创建一个新文件夹,然后将存储库从现有文件夹克隆到其中:

git clone --depth=1 /path/to/existingFolder /path/to/newFolder

Then copy the .git directory from /path/to/newFolder to /path/to/existingFolder , then delete /path/to/newFolder . 然后将.git目录从/path/to/newFolder复制到/path/to/existingFolder ,然后删除/path/to/newFolder

EDIT : according to this answer , you can edit the shallow file directly: 编辑 :根据此答案 ,您可以直接编辑shallow文件:

git show-ref -s HEAD > .git/shallow
git reflog expire --expire=0
git prune
git prune-packed

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM