THE BEST BLOG EVER

Technology

THE ULTIMATE ALL-IN-ONE GIT BASH COMMAND

A single Bash command to sync, merge, push, auto-resolve conflicts, and open PRs across every branch.

By Liyam Flexer · Published Jun 3, 2025 · 6 min read

On This Page

This is a single Bash one-liner that fetches, syncs, commits, merges, pushes, auto-resolves merge conflicts, cleans, optimizes, and even opens Pull Requests — for every branch in your repo. It collapses a long, error-prone manual Git routine into one copy-paste command, and it builds on the open-source GitHub CLI for the PR step.

The trade-off is power for safety: automatic conflict resolution and aggressive cleanup can overwrite or delete work. Read the warnings before you run it on anything you care about.

The One-Liner

Copy-paste this into a repo and run it:

git fetch --all && for branch in $(git branch | sed 's/[* ]//g'); do \
  git checkout $branch && \
  git pull origin $branch --no-rebase --allow-unrelated-histories || \
  (git ls-files -u | cut -f2 | sort -u | xargs -I{} git checkout --ours {} && git add . && git commit -am "Auto-resolve conflicts with ours in $branch" || true); \
  git add . && git commit -am "Update $branch" || true && git push origin $branch; \
done && \
git checkout main && \
for branch in $(git branch | grep -v main | sed 's/[* ]//g'); do \
  git merge $branch --no-ff -m "Merge $branch into main" || \
  (git ls-files -u | cut -f2 | sort -u | xargs -I{} git checkout --ours {} && git add . && git commit -am "Auto-resolve conflicts with ours during merge from $branch" || true); \
done && \
git push origin main && git fetch --prune && git gc && git clean -fdx && \
for branch in $(git branch | grep -v main | sed 's/[* ]//g'); do \
  gh pr create --base main --head $branch --title "PR: Merge $branch" --body "Automated PR" || true; \
done

What It Does

The command runs three passes over your repository, then a cleanup and a PR pass:

  • Fetches and updates all branches with git fetch --all
  • Stages, commits, and pushes all changes on every branch
  • Auto-resolves merge conflicts using your local code (--ours); swap for --theirs to prefer the remote
  • Merges every feature branch into main with --no-ff
  • Pushes the merged main branch
  • Cleans, prunes, and garbage-collects the repo with git fetch --prune, git gc, and git clean -fdx
  • Opens Pull Requests for every branch when the GitHub CLI is installed

Why Use It

  • Save time: no manual branch-hopping or repetitive commands
  • Reduce errors: never forget a commit, push, or merge
  • Works for any repo: paste and go
  • Useful for solo devs, teams, and power users
  • Handles merge conflicts automatically, configurable for --ours or --theirs

The Risks (Read Before Running)

This command is destructive by design. Treat each of these as a hard prerequisite:

  • Automatic conflict resolution can overwrite code. The --ours strategy discards the other side of every conflict without review. Always back up critical repos first.
  • git clean -fdx deletes untracked and ignored files, including build artifacts and local config. There is no undo.
  • git gc rewrites repository internals; let it finish.
  • Review changes and run your tests before relying on any auto-resolved merge.
  • PR automation needs gh auth login — you must be authenticated with the GitHub CLI.

How to Make It Yours

Save it as a script

Save the one-liner as git-sync-all.sh, then make it executable:

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

Alias it

Add an alias to your .zshrc or .bash_profile so the whole workflow runs from one short word:

alias gitsyncall='[the one-liner above, all on one line]'

Dry-run it first

Replace the destructive actions with echo to print the commands instead of executing them — a safe way to preview exactly what will run.

Best Practices

  • Back up first if you have uncommitted work you care about
  • Check for merge conflicts — the script tries to resolve them, but manual attention is best for critical merges
  • Review PRs and test your code before deploying
  • Customize for your default branch (main vs master)
  • Alias it in your shell for faster repeat use

The Bottom Line

This one-liner trades manual control for speed: it automates the entire fetch-commit-merge-push-PR cycle across every branch in a single command. That makes it a genuine time-saver for repetitive Git work — but the same automation that saves the time can overwrite or delete code without asking. Back up first, dry-run when unsure, and review every auto-resolved merge before you trust it.

Explore Related Concepts
Frequently Asked Questions
What is the most useful Git command to know?+

git log --oneline --graph --decorate --all is one of the most powerful single commands — it shows a visual history of all branches and commits in a compact format.

How do I combine multiple Git commands into one?+

You can chain commands with && in bash (e.g., git add . && git commit -m 'msg' && git push) or create a git alias in your .gitconfig for frequently combined operations.

What is a Git alias?+

A Git alias lets you create shorthand commands — for example, setting git lg as a shortcut for a long git log command with custom formatting.

How do I set up a Git alias?+

Run git config --global alias.shortname 'original command' to create a global alias, or add it directly under [alias] in your ~/.gitconfig file.

What does git status -sb do?+

git status -sb gives a compact, short-format status output with the current branch name — much faster to scan than the default verbose output.