Skip to content

Instantly share code, notes, and snippets.

@favila
Created October 2, 2013 20:22
Show Gist options
  • Select an option

  • Save favila/6799961 to your computer and use it in GitHub Desktop.

Select an option

Save favila/6799961 to your computer and use it in GitHub Desktop.
git-archive-branch.sh Replace a git branch with an archived tag.
#!/bin/sh
# git-archive-branch.sh
#
BRANCHNAME=
PUSH=
usage() {
cat 1>&2 << EOF
usage: $(filename $0) [-p] [-h] BRANCHNAME
"Archive" a local git branch.
Creates tag archive/BRANCHNAME pointing to the head of the branch, then
deletes the branch. BRANCHNAME must exist in your local repository.
If the option -p is provided, also pushes the tag and the deletion to the
origin repository.
EOF
}
archivebranch() {
git tag archive/$BRANCHNAME $BRANCHNAME && git branch -d $BRANCHNAME
}
pushchanges() {
git push --tags && git push origin :$BRANCHNAME
}
while getopts "ph" OPT; do
case $OPT in
p) PUSH=1;;
h) usage; exit;;
esac
shift $((OPTIND-1))
done
BRANCHNAME=$1
if [ -n "$BRANCHNAME" ]; then
archivebranch
else
usage
exit 1
fi
if [ -n "$PUSH" ]; then
pushchanges
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment