问题
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:
git reset
with an appropriate mode (hard, soft, mixed, etc).- Interactive rebasing.
- 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