DEV Community

Cover image for ๐Ÿ’ก How to Pull Updates for All Git Branches Without Switching Manually
Tahsin Abrar
Tahsin Abrar

Posted on • Edited on

๐Ÿ’ก How to Pull Updates for All Git Branches Without Switching Manually

When working on multiple local branches (say for different client tasks, features, or experiments), you often:

  • Switch to a branch using git checkout
  • Pull the latest changes using git pull
  • Repeat this for every other branch

Thatโ€™s fine for 2-3 branches. But what if you have 10, 20, or even 30?

โ€œThere has to be a better way.โ€

Spoiler: There is.


๐Ÿ› ๏ธ The Goal

We want to pull the latest changes from the remote for every local branch without manually checking each one out.

Letโ€™s automate that!


Step-by-Step Solution

Hereโ€™s how to pull updates for all branches at once:

Step 1: List All Local Branches

git branch
Enter fullscreen mode Exit fullscreen mode

This gives you a list like:

  main
* feature/login
  bugfix/footer
  refactor/api
Enter fullscreen mode Exit fullscreen mode

Great โ€” but we want to loop through these branches.


Step 2: Loop Through Branches and Pull

Hereโ€™s a handy Bash script that does the job:

#!/bin/bash

current_branch=$(git rev-parse --abbrev-ref HEAD)

for branch in $(git for-each-ref --format='%(refname:short)' refs/heads/); do
  echo "๐Ÿ”„ Checking out $branch..."
  git checkout "$branch" > /dev/null 2>&1

  echo "โฌ‡๏ธ Pulling latest changes for $branch..."
  git pull --rebase

  echo "โœ… Done with $branch"
  echo "-------------------------"
done

# Switch back to original branch
git checkout "$current_branch" > /dev/null 2>&1
echo "๐Ÿš€ All done! Back on $current_branch"
Enter fullscreen mode Exit fullscreen mode

Save this script as git-pull-all.sh and make it executable:

chmod +x git-pull-all.sh
Enter fullscreen mode Exit fullscreen mode

Run it with:

./git-pull-all.sh
Enter fullscreen mode Exit fullscreen mode

  • The script uses --rebase instead of a plain pull to keep history clean. You can remove it if you prefer merge commits.
  • Works best if all branches are tracking remote ones (e.g. origin/main, origin/feature/login etc.).
  • If you use authentication (like SSH keys or PATs), make sure you're authenticated before running the script.

Top comments (0)