Able to push to all git remotes with the one command?

Git

Git Problem Overview


Instead of doing:

git push origin --all && git push nodester --all && git push duostack --all

Is there a way to do that with just one command?

Thanks :)

Git Solutions


Solution 1 - Git

Create an all remote with several repo URLs to its name:

git remote add all origin-host:path/proj.git
git remote set-url --add all nodester-host:path/proj.git
git remote set-url --add all duostack-host:path/proj.git

Then just git push all --all.


This is how it looks in .git/config:

  [remote "all"]
  url = origin-host:path/proj.git
  url = nodester-host:path/proj.git
  url = duostack-host:path/proj.git

Solution 2 - Git

To push all branches to all remotes:
git remote | xargs -L1 git push --all
Or if you want to push a specific branch to all remotes:

Replace master with the branch you want to push.

git remote | xargs -L1 -I R git push R master
(Bonus) To make a git alias for the command:
git config --global alias.pushall '!git remote | xargs -L1 git push --all'

Running git pushall will now push all branches to all remotes.

Solution 3 - Git

If you want to always push to repo1, repo2, and repo3 but always pull only from repo1, set up the remote 'origin' as

[remote "origin"]
	url = https://[email protected]/path/to/repo1
 	pushurl = https://[email protected]/path/to/repo1
    pushurl = https://[email protected]/path/to/repo2
	pushurl = https://[email protected]/path/to/repo3
	fetch = +refs/heads/*:refs/remotes/origin/*

Configure at command line:

$ git remote add origin https://[email protected]/path/to/repo1
$ git remote set-url --push --add origin https://[email protected]/path/to/repo1
$ git remote set-url --push --add origin https://[email protected]/path/to/repo2
$ git remote set-url --push --add origin https://[email protected]/path/to/repo3

If you only want to pull from repo1 but push to repo1 and repo2 for a specific branch specialBranch:

[remote "origin"]
	url = ssh://[email protected]:7999/yyy/repo1.git
	fetch = +refs/heads/*:refs/remotes/origin/*
    ...
[remote "specialRemote"]
	url = ssh://[email protected]:7999/yyy/repo1.git
	pushurl = ssh://[email protected]:7999/yyy/repo1.git
	pushurl = ssh://[email protected]:7999/yyy/repo2.git
	fetch = +refs/heads/*:refs/remotes/origin/*
    ...
[branch "specialBranch"]
	remote = origin
    pushRemote = specialRemote
    ...

See https://git-scm.com/docs/git-config#git-config-branchltnamegtremote.

Solution 4 - Git

As a CLI Alternative to editing the .git/config file, you could use the following commands:

# git remote add all origin-host:path/proj.git
# git remote set-url --add all nodester-host:path/proj.git
# git remote set-url --add all duostack-host:path/proj.git

The same git push all --all works here as well.

You have accomplished the same as answer #1. You have just done it with Command Line instead of raw editing of the config file.

Solution 5 - Git

I wrote a short bash function to push to many remotes in one call. You can specify a single remote as a parameter, multiple remotes separated by spaces or don't specify any to have it push to all remotes.

This can be added to your .bashrc or .bash_profile.

function GitPush {
  REMOTES=$@

  # If no remotes were passed in, push to all remotes.
  if [[ -z "$REMOTES" ]]; then
    REM=`git remote`

    # Break the remotes into an array
    REMOTES=$(echo $REM | tr " " "\n")
  fi

  # Iterate through the array, pushing to each remote
  for R in $REMOTES; do
    echo "Pushing to $R..."
    git push $R
  done
}

Example: Let's say your repo has 3 remotes: rem1, rem2 and rem3.

# Pushes to rem1
GitPush rem1

# Pushes to rem1 and rem2
GitPush rem1 rem2

# Pushes to rem1, rem2 and rem3
GitPush

Solution 6 - Git

You can utilize git hooks - especially pre-push: add non-origin pushes to .git/hooks/pre-push.

Solution 7 - Git

If you already have an existing repository with origin as the default remote and want to push to multiple remote repositories by just calling git push (or with the git sync button of your IDE / text editor) first add the current origin URL (that is used for all operations e.g. push, pull, fetch) as a push-specific remote origin URL:

git remote set-url --push --add origin "$(git remote get-url --push origin)"

... and then add each other remote repository to the push-specific URLs like that:

git remote set-url --push --add origin "git@github.com:username/repo-name.git"

Now all fetch and pull operations will only fetch from your original remote repository.

But with a simple git push git pushes your latest changes to all remote repositories you've added right now.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionbaluptonView Question on Stackoverflow
Solution 1 - GitAristotle PagaltzisView Answer on Stackoverflow
Solution 2 - GitweakishView Answer on Stackoverflow
Solution 3 - GitMeng LuView Answer on Stackoverflow
Solution 4 - GitKurt VanderwaterView Answer on Stackoverflow
Solution 5 - GitKeyboardCowboyView Answer on Stackoverflow
Solution 6 - GitVitaly ZdanevichView Answer on Stackoverflow
Solution 7 - GitmiuView Answer on Stackoverflow