Cleaning up multiple local git folders

1 minute read

When working on a new software project cloning multiple repos is par for the course. On top of this, I’m a bit of clean freak in both my living space and my virtual spaces. I like a zero-inbox, my shirts are organized by season, it’s a thing. I also love it when my local repos are as up to date as possible. Cleanliness aside, it just makes sense, since if you want to start a new branch you want to start at the newest state.

Rather than manually going into each directory and pulling down the latest manually, I made a short script (aptly named that I keep in the root of the workspace. The script does what I mentioned earlier and then some.

Here’s the gist of what I want to accomplish for each repo:

  • Checkout master
  • Fetch new tags/branches
  • Delete all non-master (local) branches
  • Pull down the latest

Where I usually place the script:

$ ls
repo-foo    repo-bar    repo-baz

Nothing fancy, it meets my needs, feel free to re-use it. It’s also available on my Github.


# save current directory so we can go back after it's run

# accept a path argument, default to current dir if not supplied
if [[ -z "$path" ]]; then
    echo "no path specified, using current directory"

# output the folders we will clean
cd $path
echo "cleaning folder:"
echo $folders

# for each folder, do a few checks...
for dir in */; do
    cd $dir
    echo "cleaning folder $dir"

    # check if .git exists, it could just be a folder
    if [ -e ".git" ]; then
        git checkout -f master

        # check if there are any non-master branches to delete
        if [[ $(git branch | grep -v "master") ]]; then
            git branch | grep -v "master" | xargs git branch -D
        git fetch
        git pull origin master
        echo "skipping $dir as it's not a git project"
    cd ..

# go back home
cd $previous_dir


I’ve been using this for a while and finally thought I should share it. I hope others find it useful and can modify it to meet their needs :)