Git branch with the same name as a remote is created differently

When creating a branch with name that matches that of a remote branch the configurations for push and pull are set differently.

Having the current remote branches:

$ git branch -r
  origin/HEAD -> origin/master
  origin/master
  origin/someBranch

And creating a couple local branches tracking a remote branch:

$ git branch someBranch origin/someBranch 
  Branch someBranch set up to track remote branch someBranch from origin.
$ git branch someOtherBranch origin/someBranch 
  Branch someOtherBranch set up to track remote branch someBranch from origin.

Checking the tracking and upstream information:

$ git remote show origin
* remote origin
  Fetch URL: [email protected]:maic/repo.git
  Push  URL: [email protected]:maic/repo.git
  HEAD branch: master
  Remote branches:
    master     tracked
    someBranch tracked
  Local branches configured for 'git pull':
    master          merges with remote master
    someBranch      merges with remote someBranch
    someOtherBranch merges with remote someBranch
  Local refs configured for 'git push':
    master     pushes to master     (up to date)
    someBranch pushes to someBranch (up to date)

Why the second branch was created without the push configuration?

Upvotes: 4

Views: 9609

Answers (1)

GoZoner
GoZoner

Reputation: 70245

A local branch can only track a remote branch for pushing if the local and remote branches have the same name. For pulling, the local branch need not have the same name as the remote branch. Interestingly, if you setup a branch 'foo' to track a remote branch 'origin/bar' (with 'git branch foo origin/bar') and if the remote repository does not currently have a branch named 'foo', then if later the remote does get a branch named foo, then the local branch foo will track origin/foo thereafter!

$ git clone dev1 dev2
Cloning into 'dev2'...
done.
$ cd dev2
#
# After clone, local master tracks remote master.
#
$ git remote show origin
* remote origin
Fetch URL: /Users/ebg/dev1
Push  URL: /Users/ebg/dev1
HEAD branch: master
Remote branch:
  master tracked
Local branch configured for 'git pull':
  master merges with remote master
Local ref configured for 'git push':
  master pushes to master (up to date)
#
# Create branch br1 to track origin/master
#
$ git branch br1 origin/master
Branch br1 set up to track remote branch master from origin.
#
# br1 tracks origin/master for pulling, not pushing
#
$ git remote show origin
* remote origin
Fetch URL: /Users/ebg/dev1
Push  URL: /Users/ebg/dev1
HEAD branch: master
Remote branch:
  master tracked
Local branches configured for 'git pull':
  br1    merges with remote master
  master merges with remote master
Local ref configured for 'git push':
  master pushes to master (up to date)
#
# Go to the origin repo and now create a new 'br1' branch
#
$ cd ../dev1
$ git checkout -b br1
Switched to a new branch 'br1'
#
# Go back to dev2, fetch origin
#
$ cd ../dev2
$ git fetch origin
From /Users/ebg/dev1
 * [new branch]      br1        -> origin/br1
#
# Now local branch 'br1' is tracking origin/br1 for pushing
#
$ git remote show origin
* remote origin
Fetch URL: /Users/ebg/dev1
Push  URL: /Users/ebg/dev1
HEAD branch (remote HEAD is ambiguous, may be one of the following):
  br1
 master
Remote branches:
  br1    tracked
  master tracked
Local branches configured for 'git pull':
  br1    merges with remote master
  master merges with remote master
Local refs configured for 'git push':
  br1    pushes to br1    (up to date)
  master pushes to master (up to date)

So br1 was originally supposed to pull/push from master and we end up with br1 pulling from origin/master and pushing to a branch we never knew existed.

Upvotes: 5

Related Questions