从Git存储库和历史记录中删除目录,而不从本地文件系统中删除目录 [英] Remove directory from a Git repository, and from history, without deleting it from the local filesystem

查看:141
本文介绍了从Git存储库和历史记录中删除目录,而不从本地文件系统中删除目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我还在学习Git,并且通过将一个包含许多大文件的目录添加到我的回购中,我做了一件愚蠢的事情。现在,我想从Git中删除该目录。我知道如何使用gitignore ,我知道如何从git中删除文件,但不是从本地文件中删除文件系统,但我想将目录(及其内容)也从存储库的历史记录中取出。我在这里看到一些类似的问题,但没有一个与我的情况有关。特别是,我想保持我的本地文件系统上的目录不变。



我应该注意到,作为一个新的存储库,我还没有与其他用户分享,所以我对此时与其他用户有关的任何事情都毫不关心。

解决方案

有几种方法可以删除文件和Git的目录历史记录:


  1. git reset ,混合等)。 使用> git filter-branch >
  2. 交互式重新装订。

可能还有更多的方法(我需要更多地考虑它)。假设你在最后一次提交的Git历史记录中添加了目录,例如15次提交,那么你可以使用一个交互式重新组织。参见



如果该目录之前添加了许多提交(如超过100个) git filter-branch 可能会是一个更好的选项,运行速度更快,使用起来更方便,而不是交互式数据库超过一百次提交。



在尝试将其从存储库历史记录中删除之前,请确保复制目录及其内容,并暂时保存在Git项目目录之外。然后之后将目录移回。


I'm still learning Git, and I did a dumb thing by adding a directory full of many large files to my repo. Now, I would like to remove the directory from Git. I know how to use gitignore, and i know how to remove a file from git but not from the local filesystem but I'd like to take the directory (and its contents) out of the history of the repository as well. I see a few similar questions about this on here, but none of them seem specific to my situation. In particular, I would like to keep the directory untouched on my local filesystem.

I should note that, as a new repository, I have yet to share this with other users, so I'm unconcerned with anything that relates to other users at this point.

解决方案

There are several ways to remove file and directory history from Git:

  1. git reset with an appropriate mode (hard, soft, mixed, etc).
  2. Interactive rebasing.
  3. Using git filter-branch.

There could possibly be more methods (I would need to think more about it). Which one you use depends on your situation.

Assuming that you added the directory to your Git history in the last, say, 15 commits, you could just use an interactive rebase. See

If the directory was added many many commits ago (like more than 100), than git filter-branch would probably be a better option that will run faster and be more convenient to use than an interactive rebase over a hundred or more commits.

Make sure to make a copy of the directory and its contents and save it temporarily outside of the Git project directory, before attempting to remove it from the repository history. Then move the directory back afterwards.

这篇关于从Git存储库和历史记录中删除目录,而不从本地文件系统中删除目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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