Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Git Archive Bash Script
#!/bin/bash
# Takes one parameter: a remote git repository URL.
#
# This is the stuff this script does:
#
# 1. Clones the repository
# 2. Fetches all remote branches
# 3. Compresses the folder
# 4. Deletes the cloned folder.
#
# Your remote repository is left untouched by this script.
if [ "$1" = "" ]
then
echo "Usage: $0 <git repositiry clone URL>" 1>&2
exit 1
fi
# Variable definitions
GITURL=$1
GITNAME=${GITURL##*/}
FOLDERNAME=${GITNAME%.git}
TARNAME="$FOLDERNAME.gitarchive.$(date +%Y%m%d).tgz"
# Clone the repos and go into the folder
git clone --recursive $GITURL $FOLDERNAME
cd $FOLDERNAME
# Pull all branches
git branch -r | grep -v HEAD | grep -v master | while read branch; do
git branch --track ${branch##*/} $branch
done
#Pull all remote data and tags
git fetch --all
git fetch --tags
git pull --all
git gc # Cleanup unnecessary files and optimize the local repository
# Create an archive of the directory
cd ../
tar cfz "$TARNAME" "$FOLDERNAME/"
# Remove the git clone
rm -rf "./$FOLDERNAME"
echo "Done!"
echo "Your archived git repository is named $TARNAME"
@Neats29

This comment has been minimized.

Copy link

Neats29 commented Dec 4, 2015

Hi, I got here through searching for a way to create an alias to run git clone and cd'ing into it, in one command.
I found these two lines very helpful!

GITNAME=${GITURL##*/}
FOLDERNAME=${GITNAME%.git}

Just wanted to say thanks for putting this file out there. I'm trying to understand what ## is doing. I know ${GITURL##*/} is saying set what's after the last / as GITNAME, but what is the definition of ## as a regex character. I can't seem to find it online.
Thank you!

@RajaSrinivasan

This comment has been minimized.

Copy link

RajaSrinivasan commented Jun 2, 2016

I would like to use this script. But what is the license for this? Or is this covered by some sort of "gist" license?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.