从历史记录中完全删除(旧)git 提交

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4987341/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 05:03:26  来源:igfitidea点击:

Completely remove (old) git commits from history

gitsizehistoryrebase

提问by Thinner

I'm starting a project using git where I'll be committing very large files, but only a few times a week. I've tried to use git as-is and it seems to store the entire file in each commit where it is changed. This will not work for this project, the repository would grow out of control. So, I want to reduce the size of the repository.

我正在使用 git 开始一个项目,我将在其中提交非常大的文件,但每周只提交几次。我尝试按原样使用 git,它似乎将整个文件存储在每次更改的提交中。这对这个项目不起作用,存储库会失控。所以,我想减少存储库的大小。

My first thought was to "simply" remove all commits older than say two weeks, or only keep e.g. five commits in the history (this is probably better :)) I've googled and read a lot from The Git Community Book and I guess I'm gonna need to work with git-rebaseor git-filter-branch. The thing is I just can't seem to get it to work.

我的第一个想法是“简单地”删除所有超过两周的提交,或者只在历史记录中保留例如五个提交(这可能更好:))我需要与git-rebase或一起工作git-filter-branch。问题是我似乎无法让它发挥作用。

Just to illustrate; I have a history H with only one branch (The master branch)

只是为了说明;我的历史 H 只有一个分支(主分支)

A --> B --> C --> D --> E

A --> B --> C --> D --> E

I want to remove some previous commits to make my history look like

我想删除一些以前的提交以使我的历史看起来像

C --> D --> E

C --> D --> E

Commits A and B should be completely purged. I've tried git-rebasebut it seems to merge commits together rather than actually removing old ones, maybe I don't fully understand how rebase works.. Another thought I had was to remove everything from .git/objects and then build a new commit using git-hash-object -w, git-mktreeand git-commit-tree, I have not yet managed to push this "artificial" tree to the server though.

应完全清除提交 A 和 B。我试过git-rebase但它似乎将提交合并在一起而不是实际删除旧的提交,也许我不完全理解 rebase 是如何工作的..我的另一个想法是从 .git/objects 中删除所有内容,然后使用构建一个新的提交git-hash-object -wgit-mktree而且git-commit-tree,我还没有设法将这个“人工”树推送到服务器。

I won't be working with any branches, so there's no need taking these into account.

我不会与任何分支机构合作,因此无需考虑这些。

What I'm wondering is if anyone can give me concrete usages of git-rebaseif that's what I'm supposed to use? Or some other tips, examples of what I can do.

我想知道是否有人可以给我具体的用法,git-rebase如果那是我应该使用的?或者其他一些技巧,我可以做什么的例子。

Cheers!

干杯!


Edit:


编辑:

The large files will not be the same large files all the time, and some files will be replaced by new files. I want these replaced files to be completely purgedfrom the history.

大文件不会一直都是同一个大文件,有些文件会被新文件取代。我希望这些替换的文件从历史记录中完全清除

回答by alternative

This should be a simple git rebase -iwhere you have

这应该是一个简单的git rebase -i地方

p A
s B
s C
p D
p E

and then edit the commit message for A-C to be just C's commit message.

然后将 AC 的提交消息编辑为 C 的提交消息。

git-rebase will "squash" all the commits into a single commit, who's objects are the same as commit C's objects.

git-rebase 会将所有提交“压缩”为单个提交,谁的对象与提交 C 的对象相同。

Note: It may be possible to use git filter-branchto change the big files in the previous commits to actually match the new ones, if you'd rather do that. But its a dangerous operation and I don't want to give you a bad command on accident.

注意:git filter-branch如果您愿意,可以使用更改先前提交中的大文件以实际匹配新文件。但这是一个危险的操作,我不想在意外时给你一个糟糕的命令。