我可以从Git仓库中删除最初的提交吗? [英] Can I remove the initial commit from a Git repo?

查看:77
本文介绍了我可以从Git仓库中删除最初的提交吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

看起来我最初的提交已经占用了90%的空间,因为它意外地在其中制作了很多媒体文件。有没有办法从本地和远程回购中删除第一次提交,或者我应该离开这一次? 解决方案

这听起来像你已经与许多其他用户共享知识库。如果是这种情况,那么你可能应该忍受它。



如果你控制了所有的克隆,那么你可以重写历史记录修改后的根提交的顶部,并删除了意外的文件。请注意,如果其他开发人员已经完成了此分支的工作,则不应该这样做。



如果您要重写历史记录,则可以尝试以下操作。请注意,因为git会在日志中记录HEAD提交最近的日志(reflogs),所以即使您试图使用,大型对象也不会立即从您的存储库或其他已经拥有它们的存储库中消失, git gc git gc --prune 。但是,它将确保任何新的克隆都不会最终将大对象作为主分支历史记录的一部分。



假设您的工作目录是'clean':

 #返回初始提交
git checkout< SHA1_of_old_root>

#清理索引以删除不需要的文件,例如使用git rm< files>
#...

#修改新树的初始提交。注意新提交的$ sha $ b $ git commit --amend

#返回主分支
git checkout master

#重新申请所有提交到新根
git rebase --onto< SHA1_of_new_root> < SHA1_of_old_root>


It seems that my initial commit is eating up 90% of the space, since it accidentally got made with quite a lot of media files in it. Is there a way to remove just the first commit from local and remote repos, or should I just leave this one be?

解决方案

It sounds like you've already shared the repository with a number of other users. If this is the case, then you should probably just live with it.

If you're in control of all of the clones then you can re-write the history on top of a modified root commit with the unintended files removed. Note that you shouldn't do this if other developers have already based work off this branch.

If you're into rewriting history then you can try the following. Note that because git keeps around logs of where your HEAD commit has been recently (reflogs), the large objects won't immediately disappear from your repository, or other repositories which already have them, even if you attempt to git gc or git gc --prune. It will, however, ensure that any new clones won't end up fetching the large objects as part of the history of the master branch.

Assuming that your working directory is 'clean':

# Go back the initial commit
git checkout <SHA1_of_old_root>

# Clean up the index to remove unwanted files, e.g. using git rm <files>
# ...

# Amend the initial commit with the new tree. Note the sha1 of the new commit
git commit --amend

# Go back to the master branch
git checkout master

# Re-apply all the commits onto the new root
git rebase --onto <SHA1_of_new_root> <SHA1_of_old_root>

这篇关于我可以从Git仓库中删除最初的提交吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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