简体   繁体   中英

How to set default remote in git?

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 ?

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 orgit 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 branch cannot setup for git pull , a default remote can be set for git checkout in your repository using the checkout.defaultRemote setting. But for this to be of any real benefit, the configuration would need to be setup when you first start working on a project with multiple remotes, or as soon as you start adding additional remotes.

In my sample below I'm going to use the remote name origin instead of your specific remote named black because for most people, the origin remote is the one you'd want to setup like this. For your purposes though, just replace any instance of origin with black in the samples below.
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

The result of this command would be that each time you want to setup a new local copy of a remote branch from "origin" you only need to type:

git checkout some-branch

And git will assume you actually meant:

git checkout --track origin/some-branch

This is an alternate method of setting up a local copy from the solution mentioned above with git checkout -b some-branch and is more useful when you know the branch already exists on one or more remotes.

A tracked branch means that when you subsequently need to do a git pull , it knows which remote to pull from. If the local branch is not already setup to track a remote, or if it is setup but still want to pull code from a second remote, then you will always need to manually specify which remote to pull from with git pull <remote-name> .

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

git config --global checkout.defaultRemote origin

要自动化此分支的流程:

git config branch.<your-branch>.remote black
## For origin
git config --local checkout.defaultRemote origin

## For main
git config --local checkout.defaultRemote main

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM