Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

specify default branch for a non-default remote for pull

Tags:

git

github

I have the remote origin set as the default branch for my current branch. I also have an upstream remote which is not the default for the branch. Is there a way to configure a default branch on the remote so when I pull it defaults to that branch?

Here is my .git/config:

[remote "origin"]
        fetch = +refs/heads/*:refs/remotes/origin/*
        url = [email protected]:studgeek/knockout.git
[branch "gh-pages"]
        remote = origin
        merge = refs/heads/gh-pages
[remote "upstream"]
        url = git://github.com/SteveSanderson/knockout.git
        fetch = +refs/heads/*:refs/remotes/upstream/*
        merge = refs/heads/gh-pages

With this I can happily do the following and it defaults to origin/gh-pages

git pull

What I would like to do is just give it the remote upstream and have it figure out the branch (gh-pages) part so

git pull upstream

rather than this

git pull upstream gh-pages

Right now I get the following if I omit the branch:

$ git pull upstream
You asked to pull from the remote 'upstream', but did not specify
a branch. Because this is not the default configured remote
for your current branch, you must specify a branch on the command line.

I can see three different ways of defaulting that would work for me in my current situations, but I'm not sure how to do any of them :): * Just use the current branch as the default on the remote upstream * Indicate a default branch for the upstream remote for the current branch (while leaving origin the default branch) * Indicate a default branch on the remote. The danger here of course if I switch branches the default upstream branch stays the same. In my case that would be fine, but I can see that burning folks who didn't expect it.

Note specifying git branch for remote asks a similar question, but the solution requires doing one of two things we don't want to do - changing the default remote or explicitly listing the branch (we want this codified to avoid manual error).

like image 994
studgeek Avatar asked Oct 09 '22 23:10

studgeek


1 Answers

In the .git/config, you can supply the information. Example if you are in branch foo and you want to pull remotely from moo

[branch "foo"]
   remote = origin
   merge = refs/heads/moo

Next time you run git pull in branch foo, it will pull from moo.

This has been covered in stackoverflow => How do you get git to always pull from a specific branch?

like image 192
First Zero Avatar answered Oct 17 '22 00:10

First Zero