Skip to content

Instantly share code, notes, and snippets.

@cbednarski
Created April 7, 2016 00:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cbednarski/51f92839915cfa8a40f237fa8138dd74 to your computer and use it in GitHub Desktop.
Save cbednarski/51f92839915cfa8a40f237fa8138dd74 to your computer and use it in GitHub Desktop.
Backup github repos, courtesy @kikitux
#!/bin/bash
user="`basename ${0%.sh}`"
GHTEST="`curl -sS -I https://api.github.com 2>&1`"
RET=$?
if [ ${RET} -ne 0 ]; then
echo "ERR: check internet connection"
echo "${GHTEST}"
echo "Curl exit code: ${RET}"
exit ${RET}
fi
therepolist="`curl -sSL https://api.github.com/users/${user}/repos | jq -r .[].html_url`"
[ -d ${user} ] || mkdir -p ${user}
for repourl in ${therepolist}; do
repo="${repourl#https://github.com/}"
if [ -d ${repo} ] ;then
pushd ${repo}
#check .git/config for pr/*, add if not present
grep 'fetch.*.=.*.+refs/pull/\*/head:refs/remotes/origin/pr/\*' .git/config &>/dev/null
if [ $? -ne 0 ]; then
git config --add remote.origin.fetch '+refs/pull/*/head:refs/remotes/origin/pr/*'
fi
git clean -fdx
git checkout master
git pull -f --prune origin HEAD
git fetch --all --tags
popd
else
git clone ${repourl} ${repo}
pushd ${repo}
git config --add remote.origin.fetch '+refs/pull/*/head:refs/remotes/origin/pr/*'
git pull -f --prune origin HEAD
git fetch --all --tags
popd
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment