Is there a way to set up a git repository, so that git pull
defaults to one remote and git push
defaults to another? I know I can set both by changing the value of the remote
variable in branch section of .git/config
, but how to do it for each direction separately?
There is no default remote, each branch can track a specific branch from a remote repo. If you have created the branch using git checkout -b <branch-name> where <branch-name> is the name of a remote branch then the new branch tracks that branch (from whatever remote hosts it).
git fetch --all and git pull -all will only track the remote branches and track local branches that track remote branches respectively. Run this command only if there are remote branches on the server which are untracked by your local branches. Thus, you can fetch all git branches.
Remote-tracking branches are references to the state of remote branches. They're local references that you can't move; Git moves them for you whenever you do any network communication, to make sure they accurately represent the state of the remote repository.
In order to push your branch to another remote branch, use the “git push” command and specify the remote name, the name of your local branch as the name of the remote branch.
Since Git version 1.7.0, you can set this with:
git remote set-url --push origin https://your.push.com/blah/
Since Git 1.8.3, you can use the remote.pushDefault
option to do exactly what you want (i.e. having different default remotes for pull
and push
). You can set the option just like any other; for example, to set it to the pushTarget
remote, use
git config remote.pushDefault pushTarget
This option will have the following effect:
git pull
will pull from the remote specified by the remote
option in the relevant branch section in .git/config
, whilegit push
will push to the remote specified by remote.pushDefault
.Note that you need to specify the name of a remote, not an URL. This makes this solution more flexible than the solution involving remote.<name>.pushurl
, because (for example) you will still have tracking branches for both remotes. Whether you need or want this flexibility is up to you.
The release notes say this option was added specifically to support triangular workflows.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With