完全从历史中删除(旧)git提交 [英] Completely remove (old) git commits from history

查看:149
本文介绍了完全从历史中删除(旧)git提交的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



我的第一个想法是简单地删除所有比两周前更早的提交,或者只保留例如。在历史上有五次提交(这可能更好:))我已经从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 的具体用法,米应该使用?或者其他一些技巧,我可以做的例子。



干杯!





编辑:



大文件不会一直是大文件,有些文件会被新文件替换。我希望这些被替换的文件能够从历史记录中完全清除 这应该是一个简单的 git rebase -i 您有的地方

  p A 
s B
s C
p D
p E

然后编辑提交AC只是C的提交消息。



git-rebase会将所有提交挤压为单个提交,其对象与提交C的对象相同。注意:可以使用 git filter-branch 将先前提交中的大文件更改为以下内容:

实际上匹配新的,如果你愿意这样做。但它是一种危险的操作,我不想给你一个意外的坏命令。

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.

解决方案

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.

这篇关于完全从历史中删除(旧)git提交的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆