Skip to content

Instantly share code, notes, and snippets.

@gullradriel
Created February 25, 2026 09:19
Show Gist options
  • Select an option

  • Save gullradriel/a5b3ebf94483fbc7937f48be68ec0c87 to your computer and use it in GitHub Desktop.

Select an option

Save gullradriel/a5b3ebf94483fbc7937f48be68ec0c87 to your computer and use it in GitHub Desktop.
git "pull all" function

git_pull_all — Bash Function Documentation

Pull and prune all local Git branches in one command.


What it does

git_pull_all iterates over every local branch in the current Git repository, checks each one out, runs git pull --prune, and returns to the branch you were on when you called it.


Option 1 — Add it to ~/.bash_aliases (recommended)

Open (or create) your ~/.bash_aliases file:

nano ~/.bash_aliases

Paste the function at the bottom:

git_pull_all() {
    git_output=$(git rev-parse --abbrev-ref HEAD 2>&1)
    if [[ $? != 0 ]] || [[ -z "$git_output" ]]; then
        echo "${git_output}"
        return 1
    fi

    current_branch="${git_output}"

    for branch in $(git branch | sed -E 's/^\*/ /' | awk '{print $1}'); do
        git checkout "$branch"
        git pull --prune
        printf "\n"
    done

    echo "Done, checking out previous branch ${current_branch}"
    git checkout "$current_branch"
}

Make sure your ~/.bashrc loads the aliases file (it usually does by default, but check):

# Should already be present in ~/.bashrc:
if [ -f ~/.bash_aliases ]; then
    . ~/.bash_aliases
fi

Reload your shell to apply the changes:

source ~/.bashrc

Option 2 — Use it as a standalone script

Save the function in a file, e.g. git-pull-all.sh:

#!/usr/bin/env bash
set -euo pipefail

git_pull_all() {
    git_output=$(git rev-parse --abbrev-ref HEAD 2>&1)
    if [[ $? != 0 ]] || [[ -z "$git_output" ]]; then
        echo "${git_output}"
        return 1
    fi

    current_branch="${git_output}"

    for branch in $(git branch | sed -E 's/^\*/ /' | awk '{print $1}'); do
        git checkout "$branch"
        git pull --prune
        printf "\n"
    done

    echo "Done, checking out previous branch ${current_branch}"
    git checkout "$current_branch"
}

git_pull_all

Make it executable and run it:

chmod +x git-pull-all.sh
./git-pull-all.sh

Usage

Once loaded via ~/.bash_aliases, simply call:

git_pull_all

Run it from anywhere inside a Git repository.

Example output

Switched to branch 'develop'
Already up to date.

Switched to branch 'feature/login'
Already up to date.

Switched to branch 'main'
From github.com:you/repo
 - [deleted]  (none)  -> origin/old-branch
Already up to date.

Done, checking out previous branch main
Switched to branch 'main'

Error handling

Situation Behaviour
Called outside a Git repository Prints the Git error and returns exit code 1
A branch has no upstream set git pull will warn; the loop continues to the next branch
Uncommitted changes on the current branch git checkout may refuse to switch — commit or stash first

Notes

  • --prune removes any remote-tracking references that no longer exist on the remote, keeping your local repo clean.
  • The function saves your current branch at the start and checks it back out at the end, so you always land where you started.
  • If you have many branches or slow remotes, this can take a while — that is expected.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment