24

My .git/config:

[remote "origin"]
    url = git@github.com:nfpyfzyf/test.git
    fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
    remote = origin
    merge = refs/heads/master

My local branches:

                     HEAD
                     |
                 F---G  feature**current branch
                /
       C---D---E develop
      /          
 A---B  master

I'm now in feature branch, and want to push to remote. What is the current command, is itgit push origin feature? What will happen if I do git push?

9ilsdx 9rvj 0lo
  • 7,955
  • 10
  • 38
  • 77
nfpyfzyf
  • 2,891
  • 6
  • 26
  • 30

6 Answers6

37

To push a specific branch, run git push <remote> <branch>. In your case, your only defined remote is origin, and you want to push your feature branch, so that makes

$ git push origin feature

The “Examples” section of the git push documentation describes what happens if you run git push with no other arguments.

git push

Works like git push <remote>, where is the current branch’s remote (or origin, if no remote is configured for the current branch).

Given the configuration in your question, your feature branch does not have a remote configured, so the above invocation is equivalent to the next example.

git push origin

Without additional configuration, works like git push origin :

Following the daisy chain, we see that this is equivalent to

git push origin :

Push “matching” branches to origin. See in the OPTIONS section above for a description of "matching" branches.

The rules for matching branches are

The special refspec : (or +: to allow non-fast-forward updates) directs git to push “matching” branches: for every branch that exists on the local side, the remote side is updated if a branch of the same name already exists on the remote side. This is the default operation mode if no explicit refspec is found (that is neither on the command line nor in any Push line of the corresponding remotes file—see below) and no push.default configuration variable is set.

In your case, the only matching branch is master, so git push will push that branch and exit.

Community
  • 1
  • 1
Greg Bacon
  • 134,834
  • 32
  • 188
  • 245
2

git push origin $FEATURE is certainly correct, and will get you what you want. However, I often use:

git push -u origin HEAD

This will push up my current branch, using the branch name, so I don't have to type it out explicitly on the command line. As mentioned in one of the other answers, -u will set the upstream so that a plain git push will work in the future.

John Szakmeister
  • 44,691
  • 9
  • 89
  • 79
1

Yes git push origin feature is the right and explicit command.

What happens when you do git push without arguments is influenced by your configuration. The configuration variable push.default tells git what to do in this case. Check the man-page (git config --help and search for push.default) for more information. Just so much, there are several possibilities: nothing, matching, upstream, simple and current.

Also What is the difference between git push.default=current and push.default=upstream? can help.

Community
  • 1
  • 1
Patrick B.
  • 11,773
  • 8
  • 58
  • 101
1

I think this is possible way to do,

git push --set-upstream origin refs/heads/[your_local_branch_name]:refs/heads/[remote_branch_name_you_want]
Brian Kim
  • 11
  • 1
0

git push origin feature is correct. If you do just git push it will probably say something like this:

fatal: The current branch feature has no upstream branch.
To push the current branch and set the remote as upstream, use

    git push --set-upstream origin feature

After you have used the --set-upstream (or just -u), simple git push will work

1615903
  • 32,635
  • 12
  • 70
  • 99
0

If you're working with git-flow you can use

git flow feature publish $FEATURE

to push the branch to your remote and create it remote.

It's equal to do a simple

git push origin $FEATURE

If you're simply doing a git push it depends on your configuration what happens - in my configuration it pushes all remotely existing branches.

akluth
  • 8,393
  • 5
  • 38
  • 42