Skip to content

Instantly share code, notes, and snippets.

@dezull
Last active December 22, 2015 08:39
Show Gist options
  • Save dezull/6446211 to your computer and use it in GitHub Desktop.
Save dezull/6446211 to your computer and use it in GitHub Desktop.
Backup Git repos from GitLab/GitHab/Bitbucket.
#!/bin/bash
# Should work with GitLab/GitHub/Bitbucket
username="your-username"
host="your-host.com" # eg: github.com
clone_dir="`pwd`/repos"
repos=(
"repo-abc"
"repo-def"
)
[ -d "${clone_dir}" ] || mkdir "${clone_dir}"
for repo in "${repos[@]}"; do
url="git@${host}:${username}/${repo}.git"
repo_dir=${clone_dir}/${repo}
if [[ ! -d "${repo_dir}" ]]; then
echo "${repo}"
mkdir "${repo_dir}"
git clone "${url}" "${repo_dir}"
cd ${repo_dir}
git pull --all
# Make local branches track remote branches
for branch in `git branch -a | grep remotes | grep -v HEAD | grep -v master`; do
git branch --track ${branch##*/} $branch
done
cd - 2>&1 /dev/null
else
echo "Pulling ${repo}"
cd "${repo_dir}" && git pull --all
for branch in `git branch -a | grep remotes | grep -v HEAD | grep -v master`; do
branch_name=${branch##*/}
if ! git branch | grep --quiet ${branch_name}; then
git branch --track ${branch_name} $branch
fi
done
cd - 2>&1 /dev/null
fi
echo "----------"
echo
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment