Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Different default remote (tracking branch) for git pull and git push

Tags:

git

branch

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?

like image 996
svick Avatar asked May 26 '10 21:05

svick


People also ask

What is default remote branch git?

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).

Does git pull pull all remote branches?

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.

What are remote-tracking branches in git?

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.

Can I push to a different remote branch?

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.


2 Answers

Since Git version 1.7.0, you can set this with:

git remote set-url --push origin https://your.push.com/blah/ 
like image 114
user392887 Avatar answered Sep 20 '22 06:09

user392887


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, while
  • git 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.

like image 25
MvanGeest Avatar answered Sep 19 '22 06:09

MvanGeest