Git: which is the default configured remote for branch?
Asked Answered
C

6

219

I have a remote bare repository hub. I work only in the master branch. The last sentence of this error message below makes me wonder: How do I find out which is the "default configured remote for your current branch"? And how do I set it?

[myserver]~/progs $ git remote -v
hub     ~/sitehub/progs.git/ (fetch)
hub     ~/sitehub/progs.git/ (push)

[myserver]~/progs $ git branch -r
  hub/master

[myserver]~/progs $ cat .git/HEAD
ref: refs/heads/master

[myserver]~/progs $ git pull hub
You asked to pull from the remote 'hub', 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.
Chimb answered 31/1, 2011 at 2:19 Comment(0)
D
237

Track the remote branch

You can specify the default remote repository for pushing and pulling using git-branch’s track option. You’d normally do this by specifying the --track option when creating your local master branch, but as it already exists we’ll just update the config manually like so:

Edit your .git/config

[branch "master"]
  remote = origin
  merge = refs/heads/master

Now you can simply git push and git pull.

[source]

Delayedaction answered 31/1, 2011 at 2:28 Comment(6)
Wouldn't this also be set if the OP did git pull hub master?Galoot
@Ryan Bigg: Not automatically, or you would always screw up your config.Charleencharlemagne
Why edit a config file when git commands exist for this very reason?Gradey
git branch --set-upstream local_branch remote/remote_branch (or when pushing, as detailed below)Gradey
@scragz: No way! the command approach guarantees your .gitconfig is left in a meaningful state.Hyps
My reason for using this approach is that I had a bunch of branches so it took me less time than applying a separate command for each branch.Stanislaus
G
267

You can do it more simply, guaranteeing that your .gitconfig is left in a meaningful state:

Using Git version v1.8.0 and above

git push -u hub master when pushing, or:
git branch -u hub/master

OR

(This will set the remote for the currently checked-out branch to hub/master)
git branch --set-upstream-to hub/master

OR

(This will set the remote for the branch named branch_name to hub/master)
git branch branch_name --set-upstream-to hub/master

If you're using v1.7.x or earlier

you must use --set-upstream:
git branch --set-upstream master hub/master

Gradey answered 31/1, 2011 at 10:44 Comment(7)
For anyone wondering: the second command can be used for existing branchesFulgor
@eric-hu as detailed in my answer here: #4878749Gradey
The set-upstream[-to] command changes the currently configured remote. The original poster asked about the default configured remote. Surely that's not quite the same concept?Abortionist
@StevePitchers Does git distinguish between them? Feel free to suggest an improvement / clarification, or, indeed your own answer.Gradey
Each branch has a currently configured remote, specifying which branch on that remote corresponds to the local branch. The default configured remote determines which branch is pushed or pulled if you don't specify one explicitly. This answer only sets the current one. The accepted answer (editing by hand) also allows you to set the default one. Does anyone know a command that avoids having to edit by hand?Abortionist
--set-upstream-to made exactly the same changes in .git/config as @scragz suggested in his answer.Glennglenna
This does not explain how to find out which is the default oneBootee
D
237

Track the remote branch

You can specify the default remote repository for pushing and pulling using git-branch’s track option. You’d normally do this by specifying the --track option when creating your local master branch, but as it already exists we’ll just update the config manually like so:

Edit your .git/config

[branch "master"]
  remote = origin
  merge = refs/heads/master

Now you can simply git push and git pull.

[source]

Delayedaction answered 31/1, 2011 at 2:28 Comment(6)
Wouldn't this also be set if the OP did git pull hub master?Galoot
@Ryan Bigg: Not automatically, or you would always screw up your config.Charleencharlemagne
Why edit a config file when git commands exist for this very reason?Gradey
git branch --set-upstream local_branch remote/remote_branch (or when pushing, as detailed below)Gradey
@scragz: No way! the command approach guarantees your .gitconfig is left in a meaningful state.Hyps
My reason for using this approach is that I had a bunch of branches so it took me less time than applying a separate command for each branch.Stanislaus
C
38

For the sake of completeness: the previous answers tell how to set the upstream branch, but not how to see it.

There are a few ways to do this:

git branch -vv shows that info for all branches. (formatted in blue in most terminals)

cat .git/config shows this also.

For reference:

Chimb answered 2/6, 2014 at 15:26 Comment(0)
H
4

The programmatic version of the answer to this question is:

git branch --list "$(git branch --show-current)" "--format=%(upstream:remotename)"

This will output just the current branch's default remote name. The --show-current option will not work before Git version 2.22.0.

Hagioscope answered 7/1, 2022 at 1:7 Comment(1)
This doesn't output anything if your HEAD is in a detached state on a particular commit which is not associated with a branch name.Calvinism
C
4

Git: which is the default configured remote for a branch?

"Get" commands

For a branch named branch_name, read it out with this:

git config branch.branch_name.remote

Examples:

# main branch
git config branch.main.remote
# master branch
git config branch.master.remote

Sample output:

origin

"Set" commands

Change the default remote for a branch like this:

# for branch "branch_name", change the default remote to "remote_name"
git config branch.branch_name.remote remote_name

# Examples:
# main -> origin
git config branch.main.remote origin
# main -> upstream
git config branch.main.remote upstream

There is no output when running the set commands just above.

See all remotes

See all of your remote names and their URLs with:

git remote -v

The -v means "verbose".

Example run and output:

wiki_copy_demo.wiki$ git remote -v
origin  [email protected]:ElectricRCAircraftGuy/wiki_copy_demo.wiki.git (fetch)
origin  [email protected]:ElectricRCAircraftGuy/wiki_copy_demo.wiki.git (push)
upstream    https://github.com/nicolargo/glances.wiki.git (fetch)
upstream    https://github.com/nicolargo/glances.wiki.git (push)

git config general details

You can programmatically read out any given branch's locally-stored remote-tracking remote name via git config branch.branch_name.remote.

Assume that you have a branch named main and its remote it tracks is set to origin. In that case, your .git/config file will contain this, among other things:

[branch "main"]
    remote = origin
    merge = refs/heads/main

Running this:

git config branch.main.remote

...will therefore read out that configuration setting and return the value of remote, which is origin.

And running this:

git config branch.main.remote upstream

...will change the value of remote from origin (its previous value) to upstream.

You can use these patterns to programmatically read or write any git config variable, even ones you invent or make up yourself.

Example: this command: git config --global blametool.editor subl adds these lines to the bottom of your global ~/.gitconfig file:

[blametool]
    editor = subl

And you can read out that variable value, subl, with: git config blametool.editor.

That's how I set a blametool for my git blametool script.

Calvinism answered 17/11, 2022 at 4:55 Comment(0)
S
-1

the command to get the effective push remote for the branch, e.g., master, is:

git config branch.master.pushRemote || git config remote.pushDefault || git config branch.master.remote

Here's why (from the "man git config" output):

branch.name.remote [...] tells git fetch and git push which remote to fetch from/push to [...] [for push] may be overridden with remote.pushDefault (for all branches) [and] for the current branch [..] further overridden by branch.name.pushRemote [...]

For some reason, "man git push" only tells about branch.name.remote (even though it has the least precedence of the three) + erroneously states that if it is not set, push defaults to origin - it does not, it's just that when you clone a repo, branch.name.remote is set to origin, but if you remove this setting, git push will fail, even though you still have the origin remote

Silversmith answered 11/7, 2019 at 11:47 Comment(1)
This question was already answered with a more useful answer.Atop

© 2022 - 2024 — McMap. All rights reserved.