简体   繁体   English

为什么我的Git存储库比工作目录大得多?

[英]Why is my Git repository so much bigger than the working directory?

I just created a new repository and created the initial commit. 我刚刚创建了一个新的存储库并创建了初始提交。

The working directory is 2 GB. 工作目录为2 GB。 But the .git directory is a whopping 15 GB. 但.git目录高达15 GB。

Why is the git repository, with only a single commit, almost 8 times as large as the working directory? 为什么只有一次提交的git存储库几乎是工作目录的8倍?

Am I doing something wrong? 难道我做错了什么? Is there any way to fix this? 有没有什么办法解决这一问题?

The big repository size is because you added the contents of the ".hg" subdirectory temporarily, but did not use the data in the actual initial commit. 大存储库大小是因为您临时添加了“.hg”子目录的内容,但未使用实际初始提交中的数据。 Let's trace what happened step by step: 让我们一步一步地追踪发生的事情:

  1. git init : Creates a ".git" subdirectory with a small bit of metadata. git init :创建一个带有少量元数据的“.git”子目录。

  2. git add . : This copied all of the working tree into Git's index (aka staging area) - in other words all the files in your project, including all of the ".hg" directory files. :这将所有工作树复制到Git的索引(也称为暂存区域) - 换句话说,项目中的所有文件,包括所有“.hg”目录文件。 When we say that these have been added to the index, it means all the file contents have been added to the object storage database in ".git/objects", and the ".git/index" file has pointers to all the files. 当我们说这些已被添加到索引中时,它意味着所有文件内容都已添加到“.git / objects”中的对象存储数据库中,而“.git / index”文件具有指向所有文件的指针。

  3. git reset .hg : This removed the ".hg" subdirectory from the index. git reset .hg :这从索引中删除了“.hg”子目录。 But the objects that have been added to the storage are not removed, because other commits or index entries might have pointed to them. 但是,不会删除已添加到存储中的对象,因为其他提交或索引条目可能指向它们。 (Git currently does not track how many references point to an object. It operates with tracing garbage collection, not reference counting.) (Git目前不跟踪有多少引用指向一个对象。它使用跟踪垃圾收集,而不是引用计数。)

  4. git commit : This is the last command you performed, which copied the index into a new commit and stored that into the repository. git commit :这是您执行的最后一个命令,它将索引复制到新提交中并将其存储到存储库中。

To address your problem: 解决您的问题:

  • You can avoid the file bloat in the first place if you start with a blank repository and only add the files that you need, carefully excluding the ".hg". 如果从空白存储库开始并且只添加所需的文件,则可以首先避免文件膨胀,小心地排除“.hg”。

  • If you want to fix the problem after the fact, you can run git gc and hope that the unused objects are removed. 如果你想在事后修复问题,你可以运行git gc并希望删除未使用的对象。

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

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