Skip to content

Instantly share code, notes, and snippets.

@JdeJ
Last active October 22, 2023 16:45
Show Gist options
  • Save JdeJ/410660ab2bcc4b19d3648e9c92faf9d1 to your computer and use it in GitHub Desktop.
Save JdeJ/410660ab2bcc4b19d3648e9c92faf9d1 to your computer and use it in GitHub Desktop.
Script to fetch the current branch of all repositories on a folder
#!/bin/bash
# By default it will fetch the base branch of the current directory,
# if we want to fetch all branches, pass -all or -A param.
# Run it without downloading:
# bash <(curl -s https://gist.githubusercontent.com/JdeJ/410660ab2bcc4b19d3648e9c92faf9d1/raw/9f4a2d7e08b1e0d41d0825aad3813da4d0481bc1/git_pull.sh)
pull_all_branches() {
local directory="$1"
cd "$directory" || return
echo "Fetching in $PWD"
git fetch --all
for branch in `git branch -r | grep -v HEAD`;do
if [ -z "$(git branch --list ${branch##*/})" ]; then
echo "Checking out and pulling $branch"
git checkout --track $branch
else
git checkout ${branch##*/}
fi
git pull --rebase origin ${branch##*/}
done
echo ""
}
pull_current_branch() {
local directory="$1"
cd "$directory" || return
echo "Pulling in $PWD"
git pull
echo ""
}
base_dir=$(pwd)
for dir in "$base_dir"/*; do
if [ -d "$dir" ]; then
case $1 in
-all | -A)
pull_all_branches "$dir"
;;
current | *)
pull_current_branch "$dir"
;;
esac
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment