0

I'm working on a git repo with a couple other guys who managed to include several large DDL files in several commits. Is there a way for me to remove them from all past commits? Unfortunately, its not as easy as targeting specific commits since they are all over the place.

I'll settle for just keeping the latest commit on master and deleting everything before it.

orourkedd
  • 6,201
  • 5
  • 43
  • 66
  • you need to know the concrete commits and use this: http://stackoverflow.com/questions/1186535/how-to-modify-a-specified-commit – logoff Apr 16 '13 at 10:19

1 Answers1

3

http://git-scm.com/book/en/Git-Tools-Rewriting-History

The Nuclear Option: filter-branch

There is another history-rewriting option that you can use if you need to rewrite a larger number of commits in some scriptable way — for instance, changing your e-mail address globally or removing a file from every commit. The command is filter-branch, and it can rewrite huge swaths of your history, so you probably shouldn’t use it unless your project isn’t yet public and other people haven’t based work off the commits you’re about to rewrite. However, it can be very useful. You’ll learn a few of the common uses so you can get an idea of some of the things it’s capable of.

Removing a File from Every Commit

This occurs fairly commonly. Someone accidentally commits a huge binary file with a thoughtless git add ., and you want to remove it everywhere. Perhaps you accidentally committed a file that contained a password, and you want to make your project open source. filter-branch is the tool you probably want to use to scrub your entire history. To remove a file named passwords.txt from your entire history, you can use the --tree-filter option to filter-branch:

$ git filter-branch --tree-filter 'rm -f passwords.txt' HEAD

Rewrite 6b9b3cf04e7c5686a9cb838c3f36a8cb6a0fc2bd (21/21)

Ref 'refs/heads/master' was rewritten

The --tree-filter option runs the specified command after each checkout of the project and then recommits the results. In this case, you remove a file called passwords.txt from every snapshot, whether it exists or not. If you want to remove all accidentally committed editor backup files, you can run something like git filter-branch --tree-filter "rm -f *~" HEAD.

You’ll be able to watch Git rewriting trees and commits and then move the branch pointer at the end. It’s generally a good idea to do this in a testing branch and then hard-reset your master branch after you’ve determined the outcome is what you really want. To run filter-branch on all your branches, you can pass --all to the command.

Beware it's a powerful but also dangerous tool, tell your colleagues not to push any commits to prevent from history chaos.

Community
  • 1
  • 1
Shane Hou
  • 4,808
  • 9
  • 35
  • 50