简体   繁体   中英

How can I get git to automatically match a remote branch by name?

I'm often working with cloned repos, having an "origin" (my) and "upstream" (original source). I'm always cloning the "origin" repo from github for working on it and create PRs and from time to time need to pull from "upstream" to sync with the latest changes going on there.

After git clone <origin uri> I can do

git push/pull

without specifying the branch since its already tracked; however doing this on the other remote

git pull upstream

when eg on master branch I would like git to actually do

git pull upstream master

instead of:

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 understand I can configure the tracking remote but I'd like to know if this can be automatic as long as branch names are identical.

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.

Looking at the message, the first thing is as you haven't configured upstream as your default remote. So, first, you need to set it as the default remote like:

git config branch.branch_name.remote upstream

Now, you need to specify a branch from where you want to fetch the commits. You can specify that by running

git config branch.branch_name.merge refs/heads/branch_name

Hope it helps.

Just use --track or --set-upstream-to(-u) :

$ git branch --track master upstream/master

For the current branch:

$ git branch -u upstream/master
$ git branch --set-upstream-to=upstream/master

This'll assign the remote-tracking branch upstream/master to your master branch so that it's automatically fetched from in the next git pull invocation.

From the man page:

   -t, --track
       When creating a new branch, set up branch.<name>.remote and branch.<name>.merge
       configuration entries to mark the start-point branch as "upstream" from the new
       branch. This configuration will tell git to show the relationship between the 
       two branches in git status and git branch -v. Furthermore, it directs git pull
       without arguments to pull from the upstream when the new branch is checked out.

   -u <upstream>, --set-upstream-to=<upstream>
       Set up <branchname>'s tracking information so <upstream> is considered 
       <branchname>'s upstream branch. If no <branchname> is specified, then it defaults
       to the current branch.

您可以使用以下配置,因此,每当您从远程签出新分支时,都会自动对其进行跟踪,而无需使用--set-upstream-to--track设置跟踪

git config --global branch.autosetupmerge true

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