Cleaning Out Git Remotes The Easy Way

I had a rather large number of remote repositories set up in my Backbone.ModelBinding repository on my box, due to the wonderful community of contributors. But it was time for me to clean out the remotes as I no longer needed any of the old ones. To make it easy, I used a simple bash script:

git remote | while read a; do
> git remote rm $a
> done

This took care of removing every single remote that I had in my ‘git remote’ list … including the “origin” that I actually did want, now that I think about it. :P

But my bash-scripting-fu is terrible, so I couldn’t figure out how to not destroy all of them. I tried adding an if statement in there to check if it’s not “origin”, but couldn’t get it to work. Anyone got some better bash-chops than me, and can point out how to do this?


About Derick Bailey

Derick Bailey is an entrepreneur, problem solver (and creator? :P ), software developer, screecaster, writer, blogger, speaker and technology leader in central Texas (north of Austin). He runs - the amazingly awesome podcast audio hosting service that everyone should be using, and where he throws down the JavaScript gauntlets to get you up to speed. He has been a professional software developer since the late 90's, and has been writing code since the late 80's. Find me on twitter: @derickbailey, @mutedsolutions, @backbonejsclass Find me on the web: SignalLeaf, WatchMeCode, Kendo UI blog, MarionetteJS, My Github profile, On Google+.
This entry was posted in Command Line, Git. Bookmark the permalink. Follow any comments here with the RSS feed for this post.
  • Mark IJbema

    Though you can actually do this with bash-fu, most of the times I do this by using perl oneliners, so for instance:

    git remote | perl -pe ‘if(!/origin/){s/.*/rm $&/}else{$_=”"}’                                                                      

    And if you like like what you see, add | sh

    Personally I think Perl is just a bit more readable/writeable, without becoming to verbose (i dislike ruby for this, since it’s not that well suited for writing oneliners imho)

  • Mark IJbema

    Your comment-rss link is broken btw..

    • doh! thanks for letting me know about that. i’ll see if we can get that fixed.

  • Anonymous

    May I recommed ?


  • Anonymous

    git remote | grep -v ‘origin’ | while read REMOTE; do
    > git remote rm $REMOTE
    > done

    This would be better though:

    git remote | grep -v ‘origin’ | xargs -L1 git remote rm $*

    • i knew there was a better way than what i was doing! :)