tags:

views:

9300

answers:

4

I use the following command to push to my remote branch:

git push origin sandbox

If I say

git push origin

Does that push changes in my other branches too, or does it only update my current branch? (I have three branches: master, production and sandbox).

(The git push documentation is not very clear about this, so I'd like to clarify this for good)

What branches/remotes do the following git push commands exactly update?

git push 
git push origin

("origin" above is a remote)

(I understand that "git push [remote] [branch]" will push only that branch to the remote)

+1  A: 

A git push will try and push all local branches to the remote server, this is likely what you do not want. I have a couple of conveniences setup to deal with this:

Alias "gpull" and "gpush" appropriately:

In my ~/.bash_profile

get_git_branch() {
  echo `git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/'`
}
alias gpull='git pull origin `get_git_branch`'
alias gpush='git push origin `get_git_branch`'

Thus, executing "gpush" or "gpull" will push just my "currently on" branch.

Cody Caughlan
If you always want the behavior of gpush, you can also set remote.origin.push=HEAD (e.g. "git config remote.origin.push HEAD"), as mentioned in the examples section of the git-push man page.
Trevor Robinson
This is not necessary if you look at the above post by "Brian L".
orange80
+29  A: 

You can control the default behavior by setting push.default in your git config. From the git-config(1) documentation:

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.
  • tracking: push the current branch to its upstream branch.
  • current: push the current branch to a branch of the same name.

http://www.kernel.org/pub/software/scm/git/docs/git-config.html

Brian L
It's probably worth noting that this is new in v1.6.3: http://www.kernel.org/pub/software/scm/git/docs/RelNotes-1.6.3.txt
Charles Bailey
This "push.default" is the greatest thing ever for working with multiple repos. Set it to "tracking" and you are all good. Combined with branch --set-upstream these making push and pull way more convenient.
orange80
+6  A: 

git push origin will push all changes on the local branches that have matching remote branches at origin As for git push

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

From the Examples section of the git-push man page

docgnome
Yep, that makes it clear. I'm probably running an older version of git (1.6.1.1 Mac OS X) which does not have these examples in the man page.
Nocturne
Probably I'm running 1.6.3.1. I did find it on the site I linked however.
docgnome
So, in my case, where all the local branches have the same remote "origin", "git push" would be exactly the same as "git push origin" which would push only the local branches that have a corresponding branch in the remote.
Nocturne
@Debajit Right on! Great question by the way. I had always assumed that git push would only push the current branch. Apparently not! Very good to know.
docgnome
+2  A: 

I just put this in my .gitconfig aliases section and love how it works:

pub = "!f() { git push ${1:-origin} `git symbolic-ref HEAD`; }; f"

Will push the current branch to origin with git pub or another repo with git pub repo-name. Tasty.

matschaffer