35

I'd like to push my current branch (hp1) with

git push

and not

git push origin hp1:team/hp1

The remote branch already exists.

My local branches:

develop
master
* hp1

git remote show origin tells me:

Remote branches:
  develop  tracked
  master   tracked
  team/h2  tracked
  team/hp1 tracked
  team/n1  tracked
Local branches configured for 'git pull':
  develop  merges with remote develop
  master   merges with remote master
  hp1 merges with remote team/hp1
Local refs configured for 'git push':
  master   pushes to master   (up to date)

I already tried

git branch --set-upstream hp1 origin/team/hp1

and

git branch --set-upstream hp1 refs/remotes/origin/team/hp1

but both don't work.

My colleague has a local branch called as the remote branch (team/hp1) and the code above works for him. He gets at the end an additional

Local refs configured for 'git push':
  develop  pushes to develop  (up to date)
  master   pushes to master   (up to date)
  team/hp1 pushes to team/hp1 (up to date)

So maybe you can tell me what's wrong and how to fix it.

EDIT my config:

[core]
    repositoryformatversion = 0
    filemode = true
    bare = false
    logallrefupdates = true
[remote "origin"]
    fetch = +refs/heads/*:refs/remotes/origin/*
    url = ***@***:***.git
[branch "master"]
    remote = origin
    merge = refs/heads/master
[branch "hp1"]
    remote = origin
    merge = refs/heads/team/hp1
m1schka
  • 957
  • 1
  • 9
  • 10

4 Answers4

50

First of all, when pushing for the first time, do:

git push -u origin hp1:team/hp1

About -u option:

-u
--set-upstream

For every branch that is up to date or successfully pushed, add upstream (tracking) reference, used by argument-less git-pull(1) and other commands. For more information, see branch..merge in git-config(1).

Note from the manual that, this in itself will not determine what happens when you do git push the next time. When you do git pull while in this branch, it will fetch it from the upstream that you have set. But when you push, it will push to a matching branch ( in this case hp1 and not team/hp1)

For that to work, you have to set push.default config value to upstream. Once you set that, when you push from a branch ( just do git push), it will push to the upstream as mentioned by branch.<name>.merge

So do:

git config push.default upstream

About push.default:

push.default

Defines the action git push should take if no refspec is given on the command line, no refspec is configured in the remote, and no refspec is implied by any of the options given on the command line. Possible values are:

nothing - do not push anything.

matching - push all matching branches. All branches having the same name in both ends are considered to be matching. This is the default.

upstream - push the current branch to its upstream branch.

tracking - deprecated synonym for upstream.

current - push the current branch to a branch of the same name.

manojlds
  • 290,304
  • 63
  • 469
  • 417
  • thx, great explanation! Also, I understand now, why it is working for my colleague without setting the push.default – m1schka Nov 17 '11 at 18:25
  • Another value for push.default is simple - like upstream, but refuses to push if the upstream branch’s name is different from the local one. – MathKid Jan 11 '15 at 23:49
4

(March 2012): Beware: that "upstream" policy could become the default one soon
(sometime after git1.7.10+)
:

See "Please discuss: what "git push" should do when you do not say what to push?"

In the current setting (i.e. push.default=matching), git push without argument will push all branches that exist locally and remotely with the same name.
This is usually appropriate when a developer pushes to his own public repository, but may be confusing if not dangerous when using a shared repository.

The proposal is to change the default to 'upstream', i.e. push only the current branch, and push it to the branch git pull would pull from.
Another candidate is 'current'; this pushes only the current branch to the remote branch of the same name.

What has been discussed so far can be seen in this thread:

http://thread.gmane.org/gmane.comp.version-control.git/192547/focus=192694

Previous relevant discussions include:

To join the discussion, send your messages to: git@vger.kernel.org

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • git 2.0 will change the default behavior for git push, but only to the "simple" mode which I don't think will do what you want. See https://www.kernel.org/pub/software/scm/git/docs/git-config.html – Alexander Bird Sep 13 '13 at 22:10
  • @AlexanderBird Yes, you are right. I have documented that since: http://stackoverflow.com/a/10002469/6309, http://stackoverflow.com/a/13751847/6309. – VonC Sep 13 '13 at 22:12
1

The following will allow one not to have to specify -u ${branch_name} for the first git push.

git config "branch.${branch_name}.remote" origin
git config "branch.${branch_name}.merge" "refs/heads/${branch_name}"

Granted, it's much more typing but not when it's in a script that sets up one's workspace. It also doesn't prematurely push the branch to the remote repo.

Noel Yap
  • 18,822
  • 21
  • 92
  • 144
1

Use the -u option to git push:

$ git push -u origin hp1:team/hp1

Then, after that, you can do:

$ git push
mipadi
  • 398,885
  • 90
  • 523
  • 479