简体   繁体   English

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

[英]Completely remove (old) git commits from history

I'm starting a project using git where I'll be committing very large files, but only a few times a week. 我正在使用git开始一个项目,我将提交非常大的文件,但每周只有几次。 I've tried to use git as-is and it seems to store the entire file in each commit where it is changed. 我已经尝试使用git as-is,它似乎将整个文件存储在每次更改的提交中。 This will not work for this project, the repository would grow out of control. 这不适用于此项目,存储库将失去控制。 So, I want to reduce the size of the repository. 所以,我想减少存储库的大小。

My first thought was to "simply" remove all commits older than say two weeks, or only keep eg 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-rebase or git-filter-branch . 我的第一个想法是“简单地”删除所有比两周更早的提交,或者只保留例如历史上的五次提交(这可能更好:) :)我用谷歌搜索并阅读了很多来自The Git社区书籍,我猜我需要使用git-rebasegit-filter-branch The thing is I just can't seem to get it to work. 事情是我似乎无法让它工作。

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. 提交A和B应该完全清除。 I've tried git-rebase but 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-mktree and git-commit-tree , I have not yet managed to push this "artificial" tree to the server though. 我已经尝试了git-rebase但它似乎将提交合并在一起而不是实际删除旧的,也许我不完全理解rebase是如何工作的。另一个想法是从.git / objects中删除所有内容然后构建一个使用git-hash-object -wgit-mktreegit-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-rebase if that's what I'm supposed to use? 我想知道的是,如果有人可以使用git-rebase那么是否有人可以给我具体的用法? Or some other tips, examples of what I can do. 或者其他一些提示,我可以做的例子。

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 purged from the history. 我希望这些被替换的文件完全从历史中清除

This should be a simple git rebase -i where you have 这应该是一个简单的git rebase -i

p A
s B
s C
p D
p E

and then edit the commit message for AC 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-branch to change the big files in the previous commits to actually match the new ones, if you'd rather do that. 注意:如果您愿意,可以使用git filter-branch来更改先前提交中的大文件以实际匹配新文件。 But its a dangerous operation and I don't want to give you a bad command on accident. 但这是一次危险的行动,我不想在事故上给你一个糟糕的指挥。

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

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