Git: backup (or archive) stale remote branches

243 views Asked by At

Is there a way to backup, archive or somehow store some stale branches on our remote? We have somehow accumulated a page full of them on a project that has had people come and go over time and they branches are mostly just in the way. I'd like to delete & prune them but would prefer if I had them backed up somehow.

Thoughts?

1

There are 1 answers

0
yoshy27 On

I have created and run this automation script for that purpose since I didn't want to just convert them into stale tags.

The script generates the patch file for each branch diff. You can archive them in your preferred location.

# Fetch the remote repos
git fetch

# List no-merged remote branches
BRANCHES=`git branch -r --no-merged | awk '{print($1)}'`

# List time-sorted branches with their time, id and author
for BRANCH in $BRANCHES; 
    do echo -e `git show --format="%ci %h %an" $BRANCH | head -n 1` \\t$BRANCH;
    done | sort -r > targets

# Manually select the target lines
vim targets

# List branch names from it
BRANCHES=`cut -f 2 < targets`

# Dump the diffs of the branches
for BRANCH in $BRANCHES;
    do git diff --no-prefix -U10 HEAD...$BRANCH > ${BRANCH//\//+}.patch;
    done

# Delete the branches on the remote repo
REMOTE=origin
for BRANCH in $BRANCHES;
    do BRANCH_IN_REMOTE=$(echo $BRANCH | sed "s/$REMOTE\///g");
       echo "Deleting $BRANCH_IN_REMOTE";
       git push $REMOTE --delete $BRANCH_IN_REMOTE;
    done