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

发布于 2024-10-17 08:53:38 字数 874 浏览 1 评论 0原文

我正在使用 git 启动一个项目,其中我将提交非常大的文件,但每周只提交几次。我尝试按原样使用 git ,它似乎将整个文件存储在每次更改的提交中。这不适用于该项目,存储库将失去控制。所以,我想减小存储库的大小。

我的第一个想法是“简单地”删除所有超过两周的提交,或者只在历史记录中保留例如五个提交(这可能更好:))我已经在 google 上搜索并从 Git 社区书籍中阅读了很多内容,我想我需要使用 git-rebase 或 git-filter-branch 。问题是我似乎无法让它发挥作用。

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

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

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

C--> D——> E

提交 A 和 B 应完全清除。我尝试过 git-rebase ,但它似乎将提交合并在一起,而不是实际删除旧的提交,也许我不完全理解 rebase 的工作原理。我的另一个想法是删除 . git/objects ,然后使用 git-hash-object -w 、 git-mktree 和 git-commit-tree 构建新的提交,我但尚未设法将这个“人造”树推送到服务器。

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

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

干杯!


编辑:

大文件不会一直是相同的大文件,并且某些文件将被新文件替换。我希望这些替换的文件从历史记录中完全清除

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.

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-rebase or git-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)

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

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

C --> D --> E

Commits A and B should be completely purged. 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.

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? 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

枕花眠 2024-10-24 08:53:38

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

p A
s B
s C
p D
p E

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

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

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

This should be a simple git rebase -i where you have

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.

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

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. But its a dangerous operation and I don't want to give you a bad command on accident.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文