52

I'm quite new to Git.

I'm creating a branch and then want to push it to origin.

I think that simply issuing git push (while standing on my branch) should be sufficient.

Is it possible/reasonable to do that (by specifying push.default simple)?

Mus
  • 5,994
  • 19
  • 75
  • 112
dhblah
  • 8,755
  • 9
  • 47
  • 81
  • 3
    yes offcourse, you should be able to do git push without saying git push -u origin master. u is for setting the upstream, you should do the git push - u origin to set the upstream at least for once, and then you can do git push just like that – Brij Raj Singh - MSFT Oct 11 '13 at 07:38
  • 1
    also check this out http://stackoverflow.com/questions/948354/git-push-current-branch?rq=1 to set the push.default if you need to – Brij Raj Singh - MSFT Oct 11 '13 at 07:40

2 Answers2

44

The first push should be a:

git push -u origin branchname

That would make sure:

Any future git push will, with that default policy, only push the current branch, and only if that branch has an upstream branch with the same name.
that avoid pushing all matching branches (previous default policy), where tons of test branches were pushed even though they aren't ready to be visible on the upstream repo.

Community
  • 1
  • 1
VonC
  • 1,042,979
  • 435
  • 3,649
  • 4,283
  • Am I right at understanding that I should do two things: 1) issue `git push -u origin branchname` on first branch push 2) set push.default simple ? – dhblah Oct 11 '13 at 08:06
  • 1
    Actually I thought that git automatically links current local branch with remote branch created by issuing `git push origin branchname`. This would be reasonable for it to do so. I mean, it's kind of strange that I need to explicitly specify that local branch I'm pushing to origin should be linked with local branch I'm pushing from. – dhblah Oct 11 '13 at 08:11
  • 3
    `git push origin branchname` **does not link local and upstream branch**. You need to make that link explicit with -u. Again: http://stackoverflow.com/a/17096880/6309 – VonC Oct 11 '13 at 08:22
32

First, you need to create your branch locally

git checkout -b your_branch

After that, you can work locally in your branch, when you are ready to share the branch, push it. The next command push the branch to the remote repository origin and tracks it

git push -u origin your_branch

Your Teammates/colleagues can push to your branch by doing commits and then push explicitly

... work ...
git commit
... work ...
git commit
git push origin HEAD:refs/heads/your_branch 
carla
  • 1,728
  • 1
  • 30
  • 35
vkulkarni
  • 791
  • 1
  • 7
  • 12