Remove directory from a Git repository, and from history, without deleting it from the local filesystem

社会主义新天地 提交于 2019-12-25 03:45:06

问题


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.


回答1:


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

  • Official Linux Kernel documentation for git rebase
  • Pro Git § 6.4 Git Tools - Rewriting History

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.



来源:https://stackoverflow.com/questions/24333647/remove-directory-from-a-git-repository-and-from-history-without-deleting-it-fr

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!