Git Push and Pull from Different Remotes

A common fork-and-mirror pattern: you want to pull updates from an upstream repository (e.g., GitHub) but push your own changes to a different remote (e.g., a private Bitbucket mirror). Git handles this naturally with multiple remotes.

Setup

# Clone your private fork
git clone $BITBUCKET_REMOTE_URL $REPO
cd $REPO
 
# Add the upstream source
git remote add upstream $GITHUB_REMOTE_URL

Now:

  • git pull upstream $BRANCH — fetch and merge from GitHub
  • git push origin $BRANCH — push to Bitbucket

Alternative: single remote with split push/fetch URLs

If you prefer a single remote name, override the push URL:

git remote add origin $GITHUB_REMOTE_URL
git remote set-url --push origin $BITBUCKET_REMOTE_URL

This makes origin fetch from GitHub but push to Bitbucket.

SSH key caveat

If GitHub and Bitbucket use different SSH keys, this workflow breaks. Solutions:

  1. Use HTTPS for one or both remotes (with a credential helper).
  2. Configure the same SSH key on both services.
  3. Use a ~/.ssh/config host alias to map different keys to the same hostname (advanced).

Sources

Related: ssh, git-alternate-ssh-key