简体   繁体   English

是否可以减少 bitbucket 中的 .git 历史大小?

[英]Is it possible to reduce .git history size in bitbucket?

Since my repository size in bitbucket increased to 1GB over time, I would like to cut it and delete old history that I do not care about anymore.由于我在 bitbucket 中的存储库大小随着时间的推移增加到 1GB,我想削减它并删除我不再关心的旧历史记录。

I expect to cut my repository to atleast size of up to 100 mb (or atleast half of current) and have a history of 3-5 months.我希望将我的存储库减少到至少 100 mb(或至少当前的一半)并且有 3-5 个月的历史。 That means I'm OK to have less commits in my history.这意味着我可以在我的历史中减少提交。

This is what I have already tried and it did not work:这是我已经尝试过但没有用的方法:

  1. running git gc (with flags like --aggressive --prune ).运行git gc (带有--aggressive --prune等标志)。 It did decrease my repo size to approximately 400mb locally.它确实将我的回购大小减少到本地大约 400mb。 But I could not push it to remote, and therefore my BitBucket's repo size stayed the same.但是我无法将它推送到远程,因此我的 BitBucket 的存储库大小保持不变。 I tried using this method for Bitbucket garbage collection cleaning: https://stackoverflow.com/a/27868384/8610581 as well as https://stackoverflow.com/a/37253227/8610581我尝试使用此方法进行 Bitbucket 垃圾收集清理: https://stackoverflow.com/a/27868384/8610581以及https://stackoverflow.com/a/37253227/8610581
  2. I tried shallow clone ( git clone --shallow-since=<date> and also with --depth ) which does exactly what I need.我尝试了浅克隆( git clone --shallow-since=<date>以及--depth ),这正是我需要的。 Unfortunately, I can not make a new repository out of the shallow clone (git does not allow that).不幸的是,我不能从浅克隆中创建一个新的存储库(git 不允许这样做)。
  3. I tried reducing size of my repo by squashing many old commits into one.我尝试通过将许多旧提交压缩为一个来减小我的回购协议的大小。 I did manage to get less commits in my repo, but it did not affect my repository's size (.git folder in particular) and it remained the same.我确实设法在我的回购中获得更少的提交,但这并没有影响我的存储库的大小(特别是 .git 文件夹)并且它保持不变。
  4. Deleted unused and merged branches删除未使用和合并的分支

The only way to delete history in Git (while keeping the latest commits) is to replace the repository.删除 Git 中的历史记录(同时保留最新提交)的唯一方法是替换存储库。

The replacement repository could have the same name as the original repository, but it will have a completely different set of commits .替换存储库可以与原始存储库同名,但它将有一组完全不同的提交 The reason is that each commit is numbered (using a hash ID), and the numbering scheme incorporates the number of the previous commit.原因是每个提交都有编号(使用 hash ID),并且编号方案合并了上一个提交的编号。 So if there were, say, 1000 commits total in the old repository, and there are now only 500 commits in the new one, the commit number of the first commit in the new repository is different, which means the commit number of the final commit in the new repository is different too.因此,如果旧存储库中总共有 1000 个提交,而新存储库中现在只有 500 个提交,则存储库中第一个提交的提交编号不同,这意味着最后一个提交的提交编号在新的存储库中也是不同的。

To copy some of the old commits, and/or drop large files in the process, from the old repository to a new repository, there are a number of tools available.要复制一些旧的提交,和/或在此过程中删除大文件,从旧存储库到新存储库,有许多可用的工具。 The one that is included with Git is git filter-branch , which is in the process of being replaced by git filter-repo (but filter-repo is not yet included in Git itself). Git 包含的那个是git filter-branch ,它正在被git filter-repo替换(但 filter-repo 尚未包含在 Git 本身中)。 You can also use The BFG .您也可以使用BFG

Note that these tools generally work by this process:请注意,这些工具通常按以下过程工作:

  1. Clone the original repository.克隆原始存储库。 (Now if you damage the clone irretrievably, you just throw it away and start over!) (现在,如果你无法挽回地损坏克隆体,你只需将其丢弃并重新开始!)

  2. Use the tool on the clone.在克隆上使用该工具。 This rewrites the repository in place.这会在适当的位置重写存储库。

  3. Check to see if the result is good.检查结果是否良好。 If not, throw this clone away and go back to step 1.如果不是,则丢弃此克隆并 go 返回步骤 1。

  4. If necessary (eg, git filter-branch ) perform some finalization steps (with git filter-branch the finalization is to remove refs/original/ or clone this clone; see the documentation ).如有必要(例如git filter-branch )执行一些最终确定步骤(使用git filter-branch最终确定是删除refs/original/或克隆此克隆;请参阅文档)。

Remember that the final result is, in effect, an all-new repository.请记住,最终结果实际上是一个全新的存储库。 If you allow Git to merge it with the old repository, you'll get all the old commits—the ones you wanted to get rid of—back, and now have two copies of the commits you wanted to keep.如果您允许 Git 将它与旧存储库合并,您将获得所有旧提交(您想要删除的那些),并且现在有两个您想要保留的提交副本。

git gc only removes entries from the Git history that are no longer referenced. git gc仅从 Git 历史记录中删除不再引用的条目。 To get rid of unnecessary history entries in your repo, you have to do the cleanup in this order:要删除回购中不必要的历史条目,您必须按以下顺序进行清理:

  1. squash old commits on your master branch: This removes references to old entries but keeps the old entries around in your .git folder.压缩master分支上的旧提交:这会删除对旧条目的引用,但将旧条目保留在.git文件夹中。
  2. run git gc and git prune : this removes the old entries which are no longer referenced by your new shorter Git history from your local .git folder运行git gcgit prune :这会从本地.git文件夹中删除新的较短的 Git 历史记录不再引用的旧条目
  3. push the new smaller Git history to BitBucket: git push --force .将新的较小的 Git 历史推送到 BitBucket: git push --force Since the history is different, BitBucket refuses normal pushes and you have to use --force to overwrite the history on the server.由于历史不同,BitBucket 拒绝正常推送,您必须使用--force覆盖服务器上的历史。
  4. All other computers that also have your repo checked out, for example your CI server, are now out of sync.所有其他也检查了你的 repo 的计算机,例如你的 CI 服务器,现在都不同步了。 To get them back in sync, run git reset --hard origin/master or whatever your main development branch is.要使它们恢复同步,请运行git reset --hard origin/master或任何您的主要开发分支。
  5. Rebase all open feature branches against the new master branch so that you don't get the old history back when you merge them.根据新的master分支重新设置所有打开的功能分支的基线,以便在合并它们时不会取回旧的历史记录。

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

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