我可以从Git仓库中删除初始提交吗?

kar*_*ila 15 git

似乎我最初的提交占用了90%的空间,因为它意外地在其中包含了大量的媒体文件.有没有办法从本地和远程回购中删除第一个提交,或者我应该留下这个?

CB *_*ley 24

听起来您已经与许多其他用户共享了存储库.如果是这种情况,那么你应该只是忍受它.

如果您控制了所有克隆,那么您可以在修改后的根提交之上重新编写历史记录,并删除非预期的文件.请注意,如果其他开发人员已基于此分支工作,则不应执行此操作.

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

假设您的工作目录是"干净的":

# 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>
Run Code Online (Sandbox Code Playgroud)


归档时间:

查看次数:

9600 次

最近记录:

16 年,6 月 前