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)