0

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.

Community
  • 1
  • 1
Dylan Kinnett
  • 241
  • 3
  • 15
  • There are already questions (with answers) on Stack Overflow about how to remove history from Git repositories, you could check those out and adapt them to your situation. Screw it, digging for canonicals takes too long, I'll just answer this, and maybe someone can mark it as a duplicate later. –  Jun 20 '14 at 18:40
  • The answer to [this question](http://stackoverflow.com/questions/21230090/remove-a-deleted-folder-form-git-history) for example: will it work for a directory that has been removed but not deleted? – Dylan Kinnett Jun 20 '14 at 18:43
  • How many commits ago did you add this directory? –  Jun 20 '14 at 18:48
  • The directory was added 4 commits ago but some of the files were moved into that directory previous to that. – Dylan Kinnett Jun 20 '14 at 18:49
  • Did you want to completely erase the history of those files from before they ere moved into the directory too, or just all of their history afterwards? –  Jun 20 '14 at 18:54
  • The files are all quite large so I think the best strategy would be to completely erase the history of the files, even before they entered the directory. – Dylan Kinnett Jun 20 '14 at 18:58
  • I would handle this by first removing the history of that directory, and then removing the history of the files from before they were moved to that directory. Are they binary files? –  Jun 20 '14 at 19:00
  • yes, nearly all of them are binary files of various types. – Dylan Kinnett Jun 20 '14 at 19:01
  • Right, I guess you've now discovered that Git isn't really designed for versioning large, frequently changing binaries, right? –  Jun 20 '14 at 19:03
  • Yes, unfortunately I seem to be learning that the hard way and I hope the answer to this question will help me to ammend my ways :) – Dylan Kinnett Jun 20 '14 at 19:09
  • Please consider upvoting my answer as well if you found it helpful. –  Jun 27 '14 at 19:45

1 Answers1

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

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.