#!/bin/bash
# Text formatting
BLUE='\033[0;34m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
NC='\033[0m' # No Color
echo -e "${BLUE}Starting to pull all repositories...${NC}\n"
# Find all .git directories, excluding the .git directory itself if it exists
find . -type d -name ".git" ! -path "./.git" | while read -r gitdir; do
# Get the parent directory of the .git folder (the actual repo directory)
repo_dir=$(dirname "$gitdir")
echo -e "${BLUE}Processing${NC} ${repo_dir#./}"
cd "$repo_dir" || continue
# Get current branch
current_branch=$(git branch --show-current)
# Get default branch (usually main or master)
default_branch=$(git symbolic-ref refs/remotes/origin/HEAD 2>/dev/null | \
sed 's@^refs/remotes/origin/@@')
if [ -z "$default_branch" ]; then
# If the default branch couldn't be determined, try common names
if git show-ref --verify --quiet refs/remotes/origin/main; then
default_branch="main"
elif git show-ref --verify --quiet refs/remotes/origin/master; then
default_branch="master"
else
default_branch=""
fi
fi
# Pull current branch
echo -e "${BLUE}Pulling current branch:${NC} $current_branch"
git pull
# If current branch is not the default branch, also pull the default branch
if [ -n "$default_branch" ] && [ "$current_branch" != "$default_branch" ]; then
echo -e "${YELLOW}Also pulling default branch:${NC} $default_branch"
# Save current changes if any
git stash -q
# Switch to default branch and pull
git checkout "$default_branch"
git pull
# Switch back to original branch
git checkout "$current_branch"
# Apply stashed changes if any were stashed
git stash pop -q 2>/dev/null || true
fi
cd - > /dev/null
echo ""
done
echo -e "${GREEN}All repositories have been updated!${NC}"
Shortcut to Pull GitHub Repos
This is a MacOS tutorial on how to create a shortcut to pull all your local GitHub repositories at once.
I have a lot of repositories, and I don’t want to pull them one by one. This motivated me to create a shortcut to do this. Here’s how you can do it too.
The scripts will pull:
- Repos already on your machine,
- The default brunch (usually
main
ormaster
) of each repo, and - The current branch of each repo if it’s not the default branch.
The scripts will NOT pull:
- Any repos that are NOT on your machine, and
- Any branches that are NOT default, or NOT currently selected.
- Any repo that has unpushed changes.
In short, the scripts will only pull the repos on your machine, and up to 2 brunches (default and current) for each repo. In usual cases, it will run relatively fast and will bounce in the dock when it’s done.
1 Shell File
The first thing you need is a shell file that pulls all your repositories. Here’s the code:
Save it as pull-all-repos.sh
in the root folder of your GitHub repo. I saved it here:
If you set up your GitHub desktop long time ago (before 2021), your repos are authenticated via HTTPS, which is no longer supported. A workaround is to make SSH keys, and save your SSH passphrase in the keychain (otherwise you’ll be prompted every time you pull your repo). You can follow the instructions here.
Once you are done with the SSH key setup, try to run the .sh
file in the terminal to see if it works. If it does, you can proceed with the Automator and Quick Action setup.
2 Automator App
Secondly, we need an Automator application:
- Open Automator and select “Application” as the type of document.
- In the search bar on the left, type “shell”. You’ll find “Run Shell Script”.
- Double click on “Run Shell Script” from the actions list. It will appear on the workflow area on the right.
- In the shell script box, enter:
cd ~/Documents/GitHub # Change to your GitHub repo dir
./pull-all-repos.sh
- Save it as an application and move it into your “Application” folder. Mine has the name being “Pull All Repos”.
3 Quick Action
Thirdly, we need a Quick Action created by Automator:
- Open Automator and select “Quick Action” as the type of document.
- In the search bar on the left, type “launch”. You’ll find “Launch Application”.
- Double click on “Launch Application” from the actions list. It will appear on the workflow area on the right.
- The application will be “Contacts” by default. Change it to the “Pull All Repos” app.
- Save it. Mine has the name being “Pull All Repos”.
You’ll not see the saved directory, but if you wanna access it, it’s here:
/Users/<Your User Folder>/Library/Services
4 Keyboard Shortcut
The last step is to set up a keyboard shortcut to run the Quick Action:
- Open System Settings and scroll down to “Keyboard”.
- Click on “Keyboard Shortcuts…”.
- Select “Services” on the left, and then open the “General” dropdown menu on the right. You’ll see the “Pull All Repos” here.
- Click on the white space on the right and assign your preferred shortcut.
The first time you run this, you’ll be prompted:
zsh:2: permission denied: ./pull-all-repos.sh
You need to run these codes in your Terminal:
cd ~/Documents/GitHub # Change to your root dir of GitHub repo
sudo chmod +x pull-all-repos.sh
5 Trigger the Shortcut
Now, you can trigger the shortcut by pressing the keyboard shortcut you’ve set up. It will pull all your repositories at once. Once it’s done, your’ll see the Application Folder bounces in the dock (if you have it there).
That’s it! You’ve created a shortcut to pull all your GitHub repositories at once. Enjoy! 🚀