如何从初始提交中删除目录:推入github失败

发布于 2025-01-31 16:44:16 字数 456 浏览 3 评论 0原文

我想在github上放置我的遗产代码。以愚蠢的举动,我选择了整个项目(以pycharm)添加到初始提交中。我忘记了这包括巨大且超过任何文件和回购大小限制(8+ GB)的数据和绘图目录。我曾经希望我以后只能删除不必要的文件和目录,但是:

最初推向GitHub失败了,我似乎无法使用git git revertgit git reset 和我在这里找到的其他方法,因为以前没有提交返回的提议。

我不想冒我的代码风险,所以我转向您:如何

  1. 从commit 中删除有问题的目录和其中的文件,但不是我的磁盘 ,还是
  2. 抛弃此(本地)存储库,然后将一个新的存储库连接到Github,在我不包括这些目录中的Github?

我在这里浏览了很多答案,但我似乎找不到一个答案。这很明显吗?谢谢大家的帮助! :)

I want to put a legacy code of mine on GitHub. In a foolish move, I selected the whole project (in PyCharm) to add to the initial commit; I forgot that this includes data and plot directories that are huge and exceed any file and repo size limit (8+ GB). I had hoped I could just remove the unnecessary files and directories later on, but:

The initial push to GitHub fails, and I seem to not be able to use git revert, git reset and other methods I found here, since there is no previous commit to go back to.

I don't want to risk my code, so I turn to you: how to I either

  1. remove the offending directories and the files therein from the commit but not from my disk, or
  2. ditch this (local) repository and make a new one to connect to GitHub, where I do not include these directories from the get-go?

I looked through many, many answers here, and I just don't seem to find one. Is it that obvious? Thank you all for your help! :)

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

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

发布评论

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

评论(1

蛮可爱 2025-02-07 16:44:16

假设您只有一个提交(“初始提交”):

git rm --cached files to remove
git commit --amend

这只能从提交中删除文件,但将其留在文件系统上。

如果您在初始提交的基础上已经有不同的提交,请创建一个fixup commit以删除这些文件,然后进行交互作用:

git rm --cached files to remove
git commit --fixup=commit_id_of_initial_commit
git rebase -i --autosquash --root

我不能100%确定rebasing是否可以意外删除这些文件。为了安全起见,请先创建文件备份或将其移至其他目录。 (如果它们是从FS中消失的,则它们仍应以斑点对象存在于对象存储中,但是恢复它们将需要一些工作)。

Assuming you only have a single commit ("the initial commit"):

git rm --cached files to remove
git commit --amend

This will remove the files only from the commit, but leave them on the file system.

If you already have different commits on top of your initial commit, create a fixup commit removing those files and then rebase interactively:

git rm --cached files to remove
git commit --fixup=commit_id_of_initial_commit
git rebase -i --autosquash --root

I'm not 100% sure if rebasing could remove those files accidentally. To be on the safe side, create a backup of the files first or move them to a different directory. (If they are gone from the FS they should still exist in the object store as blob objects, but restoring them will require some work).

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