Skip to content

Instantly share code, notes, and snippets.

@jknopp
Created March 15, 2022 22:47
Show Gist options
  • Save jknopp/9bfe1303ea3e83c81e0651af12bc1b0e to your computer and use it in GitHub Desktop.
Save jknopp/9bfe1303ea3e83c81e0651af12bc1b0e to your computer and use it in GitHub Desktop.
# Push to multiple remotes
# To do this, choose a remote ID which will refer to all the remotes.
# I usually call it all, but there are developers who prefer origin.
# The idea is to add all the remote repo URLs as “push URLs” to this remote.
# Here’s what you do:
# Create a new remote called "all" with the URL of the primary repo.
git remote add all git@github.com:jknopp/toggl2redmine.git
# Re-register the remote as a push URL.
git remote set-url --add --push all git@github.com:jknopp/toggl2redmine.git
# Add a push URL to a remote. This means that "git push" will also push to this git URL.
git remote set-url --add --push all git@bitbucket.org:jknopp/toggl2redmine.git
# If you don’t want to create an extra remote named all, you can skip the first command
# and use the remote origin instead of all in the subsequent command(s).
# Now, you can push to all remote repositories with a single command!
# Replace BRANCH with the name of the branch you want to push.
git push all BRANCH
#--------------------------------------------------------------------------------------------------#
# Pull from multiple remotes
# It is not possible to git pull from multiple repos.
# However, you can git fetch from multiple repos with the following command:
git fetch --all
#This will fetch information from all remote repos.
# You can switch to the latest version of a branch on
# a particular remote with the command:
# Checkout the branch you want to work with.
git checkout BRANCH
# Reset the branch to match the state as on a specific remote.
git reset --hard REMOTE-ID/BRANCH
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment