How to set default remote in git?
Asked Answered
A

3

50

I have multiple remotes in my git repository. If I execute git pull, then it asks for the password of one of my colleagues.

How can I change the remote to black?

Arawakan answered 7/3, 2019 at 14:1 Comment(6)
git pull {originName}? you could also move things around so that origin is yours.Chirlin
@DanielA.White, I want to simply call git pull not git pull blackArawakan
origin is the name of a remote (the one where the repo was cloned from). There is no way to have more than one origin.Stableboy
@axiac, I see. I changed the question title accordingly.Arawakan
Try git config branch.<name>.remote black. name is the branch you are on now.Johathan
@ElpieKay, thx! That workedArawakan
S
42

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

If you created the branch locally then used git push --set-upstream <remote-name> <branch-name> then the local branch <branch-name> tracks the remote branch <remote-name>/<branch-name>.

You can always use git branch --set-upstream-to to change the remote branch that is tracked by the current branch or git branch --unset-upstream to tell it to not track any remote branch.

Stableboy answered 7/3, 2019 at 14:14 Comment(2)
how can I push/pull from a remote/branch that's not the upstream? In my case, I have my master branch on github, but I also have a bare repository on my network drive that has master. I've fetched everything from both and want to fast-forward master to the bare repository since that's the most up-to-date CURRENTLY... while leaving the upstream as is. Do I have to change the upstream twice or is there a shorter way to do that?Backdate
wait, I'm just being dumb. git merge desktop/masterBackdate
D
51

Edit: Thanks for all the upvotes!

In addition to the accepted answer to this question from @axiac that describes how to set the remote repository for an existing branch in your local copy, I felt it would be worthwhile to expand on this, by noting that while a default remote cannot be setup for git pull, a default remote can be set for git checkout in your repository using the checkout.defaultRemote setting. This way, git checkout some-branch, git push, and git pull will all Just Work™ the way you intended, without specifying or thinking about the remote name at all. Read on for a full explanation.

Note to OP: In my examples below I'm going to use the remote name origin instead of your specific remote named black because for most people, the default origin remote is the one you'd want to setup like this. For your purposes though, just replace any instance of origin with black.

Simplify your branch checkout process:

First, you'd type the following in the console:

git config checkout.defaultRemote origin

This command adds the following section to your repository's .git/config file:

[checkout]
     defaultRemote = origin

Now, with a default branch configured, each time you want to setup a new local copy of a remote branch that exists on the "origin" remote repository, you only need to type:

git checkout some-branch

And git will assume you actually meant:

git checkout --track origin/some-branch

Which can also be shortened to:

git checkout -u origin/some-branch

This is an amazing lifehack. It is a much smarter alternative method of setting up a local copy than the solution mentioned above with git checkout -b some-branch and is so very useful when you actually know that the branch already exists on one or more remotes. Particularly, if you're working with multiple remotes and not using defaultRemote, you would need to type the long form git checkout --track origin/branch-name each time you checkout a branch, because git doesn't know from which remote it should track. Save yourself a headache and just set this up always. (See final thoughts below for a suggested approach).

Simplify your git push and git pull process:

As hinted at above, having your local branches auto-configured to track from the defaultRemote means that when you need to do a git pull while on some branch you checked out, git already knows exactly which remote to pull from. This is very, very helpful.

Note: if the local branch tracks one remote and you want to pull code from a different remote, or if the local branch is not setup to track a remote at all, then you can either:

  • manually specify the correct remote to pull from:

      git pull <remote-name>
    

    This must be done each time you want to pull from the remote.

  • fix the remote tracking branch permanently on a branch-by-branch basis using:

      git branch --set-upstream-to <remote-name/branch-name>
    

    OR

      git config branch.<branch-name>.remote <remote-name>
      git config branch.<branch-name>.merge refs/heads/<branch-name>
    

    This is a one-time change; all subsequent git pull commands should now pull from the remote/branch you specified.

Caveats and implications:

For git config checkout.defaultRemote origin to be of any real benefit for a git pull command scenario, this command/configuration needs to be set up when you first start working on a project with multiple remotes, or as soon as you start adding additional remotes. The reason is because this configuration will only have an effect on branches you're going to check out after having set this configuration; it doesn't have any effect on existing branches. In which case, all existing local branches that don't already track from a specific remote will need to be modified separately to add the intended remote before a simple git pull will work.

Final thoughts

If you find yourself hitting this issue often on various projects, a more comprehensive setting would be to work under the convention that the default remote should always be added as the "origin" remote in all your projects. You can then set this configuration globally with:

git config --global checkout.defaultRemote origin
Danettedaney answered 17/3, 2020 at 14:47 Comment(2)
Why does the config need to be setup immediately? What would happen if you set it up later?Desilva
Only that doing git checkout some-branch wont necessarily set it up with a remote, which means more commands later in your git process.Danettedaney
S
42

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

If you created the branch locally then used git push --set-upstream <remote-name> <branch-name> then the local branch <branch-name> tracks the remote branch <remote-name>/<branch-name>.

You can always use git branch --set-upstream-to to change the remote branch that is tracked by the current branch or git branch --unset-upstream to tell it to not track any remote branch.

Stableboy answered 7/3, 2019 at 14:14 Comment(2)
how can I push/pull from a remote/branch that's not the upstream? In my case, I have my master branch on github, but I also have a bare repository on my network drive that has master. I've fetched everything from both and want to fast-forward master to the bare repository since that's the most up-to-date CURRENTLY... while leaving the upstream as is. Do I have to change the upstream twice or is there a shorter way to do that?Backdate
wait, I'm just being dumb. git merge desktop/masterBackdate
M
4

To automate the process for this branch :

git config branch.<your-branch>.remote black
Minneapolis answered 7/3, 2019 at 14:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.