How to set default remote in 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).

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.

In addition to the response above 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:

     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>


      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

To automate the process for this branch :

git config branch.<your-branch>.remote black