Pull and prune all local Git branches in one command.
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.
Open (or create) your ~/.bash_aliases file:
nano ~/.bash_aliasesPaste 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
fiReload your shell to apply the changes:
source ~/.bashrcSave 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_allMake it executable and run it:
chmod +x git-pull-all.sh
./git-pull-all.shOnce loaded via ~/.bash_aliases, simply call:
git_pull_allRun it from anywhere inside a Git repository.
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'
| 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 |
--pruneremoves 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.