Skip to content

Instantly share code, notes, and snippets.

@lexicalunit
Created October 2, 2023 20:09
Show Gist options
  • Save lexicalunit/f0abd9427ae9a19299bc73e64512da2c to your computer and use it in GitHub Desktop.
Save lexicalunit/f0abd9427ae9a19299bc73e64512da2c to your computer and use it in GitHub Desktop.
git-arc script for archiving and restoring branches
#!/bin/bash
usage() {
(
echo "usage: ${0##*/} COMMAND [args] [-h|--help]"
echo "Tool for archiving and restoring git branches."
(
echo " -h, --help@ show usage help"
echo " create BRANCH@ create an archive ref for the given branch"
echo " list@ list archived branches"
echo " delete REF@ delete the archive ref"
echo " restore REF@ restore the given ref"
) | column -ts@
) >&2
exit 1
}
if echo "$*" | grep -Eq -- '--help\b|-h\b'; then
usage
fi
case "$1" in
create)
arg="$2"
if [[ -z $arg ]]; then
echo "$(tput bold)error: please provide a branch name to archive$(tput sgr0)" >&2
exit 1
fi
git update-ref "refs/archive/$arg" "origin/$arg"
git branch -D "$arg"
;;
list)
git for-each-ref refs/archive
;;
delete)
arg="$2"
if [[ -z $arg ]]; then
echo "$(tput bold)error: please provide a ref name to delete$(tput sgr0)" >&2
exit 1
fi
git update-ref -d "$arg"
;;
restore)
arg="$2"
if [[ -z $arg ]]; then
echo "$(tput bold)error: please provide a ref name to restore$(tput sgr0)" >&2
exit 1
fi
git branch "$(echo "$arg" | cut -c14-)" "$arg"
git update-ref -d "$arg"
;;
*)
usage
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment