#!/bin/sh # git-local <--upload|--download|--remove|--list> [remote] [prefix] # # Upload (backup) and download (restore) the state of local branches to a remote # repository. This feature is useful at the end of a work session to push # changes not ready to publish to others for the purposes of backup, and to # provide some desktop independence for the developer. # # Default remote is 'origin', default prefix is current username. # # --upload # upload all local branches to remote repository with a namespace prefix # of prefix/. # # --download # download local branches on remote repository with a namespace prefix of # prefix/. # # --remove # remove all branches on remote repository with a namespace prefix of # prefix/. # # --list # list all branches on remote repository with a namespace prefix of # prefix/. # usage() { cat <<+EOF+ >&2 usage: $0 <--upload|--download> [remote] [prefix] Back up and restore local branches to a remote repository --upload local branches to remote, using prefix prefix --download branches on remote using prefix prefix as local branches --remove remote branches on remote having prefix prefix (CAREFUL!) --list remote branches on remote having prefix prefix (CAREFUL!) Default remote is 'origin', default prefix is user's username. +EOF+ } upload() { git push "$remote" "+refs/heads/*:refs/$prefix/*" } download() { git fetch -uf "$remote" "+refs/$prefix/*:refs/heads/*" git reset --hard } remove() { for b in $(git ls-remote $remote refs/$prefix/* | awk '{ print $2 }'); do git push "$remote" ":$b" done } list() { git ls-remote $remote "refs/$prefix/*" } # MAIN unset op case "$1" in --upload) op=upload ;; --download) op=download ;; --remove) op=remove ;; --list) op=list ;; *) usage exit 1 ;; esac unset remote unset prefix shift if [ $# -eq 1 ]; then remote=$1 elif [ $# -eq 2 ]; then remote=$1 prefix=$2 elif [ $# -gt 2 ]; then usage exit 1 fi if [ -z "$remote" ]; then remote=origin fi if [ -z "$prefix" ]; then prefix=$(whoami) fi case $op in upload) upload ;; download) download ;; remove) remove ;; list) list ;; *) usage exit 1 ;; esac