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

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



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 clone <origin uri>


git push/pull



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


git pull upstream



when e.g. on master branch I would like git to actually do


master


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.




3 Answers
3



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


upstream


git config branch.branch_name.remote upstream


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



Hope it helps.



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


--track


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


upstream/master


master


git pull



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.



You can use below config, so whenever you checkout a new branch from remote, it will be automatically tracked, without needing you to setup tracking with --set-upstream-to or --track


--set-upstream-to


--track


git config --global branch.autosetupmerge true





Unfortunately not: error: invalid key: branch.autosetupmerge=true
– andig
Aug 8 at 9:46





@andig there was a mistake in the command. Try now.
– Lahiru Chandima
Aug 8 at 10:02





Still no: same original message on git pull upstream
– andig
Aug 8 at 10:21


git pull upstream





@andig it will only setup tracking when you newly checkout a remote branch which does not already exist in your local copy. If all your changes are pushed to remote, you can delete your local branch and check out the branch again, and then try. Any new branch you freshly checkout in the future will have tracking setup for you.
– Lahiru Chandima
Aug 8 at 10:35





Updated question- then it's not what I'm looking for since I always clone from origin but what the "matching" behaviour when pulling from "upstream", too?
– andig
Aug 8 at 10:40






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

Creating a leaderboard in HTML/JS