15

Someone by accident just commited all of their bin and obj folders to our repo (there are around 40 such folders). I would like to do a git rm -r on all of these folders. Is there a command to do this?

Nick Volynkin
  • 14,023
  • 6
  • 43
  • 67
Jacko
  • 12,665
  • 18
  • 75
  • 126

3 Answers3

26

Have backups,

 find . -type d -name bin -exec git rm -r {} \;

 find . -type d -name obj -exec git rm -r {} \;

Update

With bash, you can set the shopt globstar, and be happy:

 shopt -s globstar
 git rm -r **/{obj,bin}/

Finally, if you need to remove these from the history of the repository, look at git filter-branch and read the section on 'Removing Objects' from the Pro Git Book

sehe
  • 374,641
  • 47
  • 450
  • 633
  • 1
    Added reference to filter-branch and the pro git book, bash globstar option – sehe May 03 '11 at 14:16
  • 1
    +1 for showing the 2-asterisk *globstar* which was new to me! – Daniel Böhmer May 03 '11 at 14:19
  • -1 for not using git clean. It was meant for these situations. – Adam Dymitruk May 03 '11 at 14:22
  • Interactive rebase is better than filter branch in this situation. – Adam Dymitruk May 03 '11 at 14:26
  • Thanks, sehe!! That did the trick. Now I need to revoke someone's repository privileges :) – Jacko May 03 '11 at 14:28
  • @adymitruk: Interactive rebase could work here, IFF there was a single commit that ONLY introduced the binaries. If not, you'd have to go back in history to amend the commit and then rebase the rest of the original branch on top of that again .... hassle. – sehe May 03 '11 at 14:33
  • 1
    Not true. If it included other files that contain proper changes, mark the commit for edit. Unstage the obj/bin folder files and rebase --continue. Problem solved. I can't believe this answer got the tic. Such a fail for not using Git and resorting to "find". – Adam Dymitruk May 03 '11 at 15:51
  • No need to be so aggressive, IMHO. Good thing you point out that edit refers to contents, not the commit message (as I've been falsely understanding that). Well, then, I agree rebase is a (slightly) better fit here. Thanks again – sehe May 03 '11 at 16:05
  • On `OSX 10.10.2`, I get `find: illegal option -- t`. That's because you need to specify the starting directory immediately after `find`, so it's: `find . -type d -name bin -exec git rm -r {} \;` – 7stud Dec 29 '15 at 13:29
  • @7stud that works on all platforms with less GNU-y extended find. Adding that. – sehe Dec 29 '15 at 13:30
  • 1
    Wow! After 4 years, you can still snap respond to questions! – 7stud Dec 29 '15 at 13:32
5

Once you revert (will keep files in history) or reset the commit,

git reset --hard

Once these are ignored files,

git clean -xdf

I use that to clean up before rebuilding a solution. Seems vs uses some dlls even after a checkout of a different branch or a merge.

You shouldn't need to resort to filter branch. Interactive rebase will do. Remember the --preserve-merges flag.

Hope this helps.

Adam Dymitruk
  • 124,556
  • 26
  • 146
  • 141
-3

Another option is to revert the offending commit with git revert.

Mike Mazur
  • 2,509
  • 1
  • 16
  • 26