DEV Community

Shawon Saha
Shawon Saha

Posted on

How to Push and Sync Every Local Git Branch in a Single Terminal Command

When juggling multiple feature experiments, prototype branches, and quick hotfixes across a complex project, you often end up with a dozen local branches that haven't been pushed to GitHub.

Running git checkout <branch> followed by git push -u origin <branch> for ten different branches is tedious and error-prone.

Here is the exact one-line command to sync every local branch, commit pending work, and set up upstream tracking automatically.


The TL;DR: The One-Liner

If you just want the magic command to run in your terminal:

git push --all origin -u
Enter fullscreen mode Exit fullscreen mode

What This Does:

  1. git push: Sends commits to the remote repository.
  2. --all: Pushes every local branch under refs/heads/, not just the one currently checked out.
  3. origin: The target remote repository.
  4. -u (or --set-upstream): Configures each branch so future git pull and git push commands know their remote counterpart without extra arguments.

What If You Have Uncommitted Changes Too?

git push --all origin -u only pushes commits that already exist. If you have uncommitted changes on your active branch and want a single command that stages, commits, and pushes everything, chain it with:

git add -A && git commit -m "chore: sync all updates" && git push --all origin -u
Enter fullscreen mode Exit fullscreen mode

(If there are no uncommitted changes, the commit step will safely exit with a notice, so you can make it resilient by using git diff-index --quiet HEAD || ...).


The Ultimate Automation: A Custom Git Alias

Remembering long bash strings or typing them out repeatedly slows you down. Turn this into a native Git command by adding an alias:

Step 1: Add the Alias to Your Global Git Config

Run this once in your terminal:

git config --global alias.sync-all '!f() { \
  if ! git diff-index --quiet HEAD --; then \
    echo "Staging & committing local changes..."; \
    git add -A && git commit -m "${1:-chore: sync all updates}"; \
  fi; \
  echo "Pushing all local branches and setting upstream..."; \
  git push --all origin -u; \
}; f'
Enter fullscreen mode Exit fullscreen mode

Step 2: Use It Anytime

From any Git repository on your machine, simply run:

git sync-all
Enter fullscreen mode Exit fullscreen mode

Or pass a custom commit message:

git sync-all "feat: sync experiments and docs"
Enter fullscreen mode Exit fullscreen mode

How It Works Under the Hood

1. git push --all vs. standard git push

Standard git push operates based on your push.default setting (usually simple), which only pushes the active branch.

Using --all instructs Git to match all local refs in .git/refs/heads/ and mirror them to the remote server under the same name:

  • refs/heads/feature-a ➔ refs/remotes/origin/feature-a
  • refs/heads/hotfix ➔ refs/remotes/origin/hotfix

2. Upstream Tracking (-u)

Without -u, branches pushed via --all may be pushed to GitHub, but your local repository might not associate them with origin/<branch>. Adding -u writes entries into .git/config:

[branch "main-optimized-v2.1"]
    remote = origin
    merge = refs/heads/main-optimized-v2.1
Enter fullscreen mode Exit fullscreen mode

This guarantees that running git status on any branch will immediately show whether you are ahead or behind the remote.


Pro Tip: Pushing Tags Too

If your workflow also relies on local git tags (such as release versions or milestones), git push --all deliberately ignores tags. To push both branches and tags in one shot:

git push --all origin -u && git push --tags origin
Enter fullscreen mode Exit fullscreen mode

Summary Cheat Sheet

Task Command
Push all local branches + link upstream git push --all origin -u
Commit active edits + push all branches git add -A && git commit -m "sync" && git push --all origin -u
Push everything including tags git push --all origin -u && git push --tags origin
Permanent 1-word shortcut git sync-all (after configuring alias)

Top comments (0)