Default behavior of "git push" without a branch specified
Asked Answered
D

12

1540

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.

Which branches and remotes do the following git push commands update exactly?

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.

Donau answered 4/6, 2009 at 2:45 Comment(5)
Regarding the configuration of diff tools in general, and the new script git difftool, I have added a new answer in this other SO question: stackoverflow.com/questions/255202/…Nissensohn
I did a blog post about the surprising behaviour of git push, which might be of interestStriker
@Mark: in other work, pushing only the current branch to its tracked upstream. Nice.Nissensohn
https://mcmap.net/q/12648/-warning-push-default-is-unset-its-implicit-value-is-changing-in-git-2-0/2157640Nutwood
help.github.com/articles/pushing-to-a-remote putting this link here for immediate help to novices like meSpecie
A
1797

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: (default before Git 2.0) push all matching branches

    All branches having the same name in both ends are considered to be matching.

  • upstream: push the current branch to its upstream branch (tracking is a deprecated synonym for upstream)

  • current: push the current branch to a branch of the same name

  • simple: (new in Git 1.7.11, default since Git 2.0) like upstream, but refuses to push if the upstream branch's name is different from the local one

    This is the safest option and is well-suited for beginners.

The simple, current and upstream modes are for those who want to push out a single branch after finishing work, even when the other branches are not yet ready to be pushed out

Command line examples:

To view the current configuration:

git config push.default

To set a new configuration:

git config push.default current
Antonetteantoni answered 4/6, 2009 at 2:46 Comment(21)
It's probably worth noting that this is new in v1.6.3: github.com/git/git/blob/master/Documentation/RelNotes/1.6.3.txtBenoni
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.Waftage
According to the git doc 'git push origin HEAD' should also work.Smidgen
"tracking" is the deprecated synonym for "upstream": kernel.org/pub/software/scm/git/docs/git-config.htmlGrownup
Note that the "upstream" (aka "tracking") setting may not necessarily push to the remote. Suppose you create a local mybranch from your local master and you have automatic tracking set up so that master is automatically set upstream of mybranch. Even after you create origin/mybranch, your local mybranch will still have your local master as its upstream branch, and that's what it'll push to. You'll need to use git branch --set-upstream to fix it.Ailurophile
The upstream synonym isn't available in Git 1.7.0.4 (the version in Ubuntu 10.04 LTS).Flann
@Joey Adams: in 1.7.0.4 just use tracking (upstream does same as tracking in old versions).Englishry
Does anybody know if it is possible to change the default behaviour of the push command only in ONE SINGLE PUSH, that is, without having to edit the config file? Something like "git push origin --global push.default upstream" (I tried this and it did not work)??Aparejo
It's worth noting that as of Git 1.7.11, there's a new simple mode. This mode is intended to become the default in future. simple works like upstream, but like current requires that the branch names are the same on both ends.Alfilaria
It's also worth noting that the config values here (upstream, current, etc) are not refspecs, and can't be combined with refspecs. For example, when I wanted to configure git to push the current branch and all tags on 'git push' I couldn't use push.default=current. Instead my .git/config file looks a bit like this: [remote "origin"] url = [email protected]:my/repo.git push = refs/tags/*:refs/tags/* push = headPrincipate
@Tiago , you can push a single branch without modifying the config by explicitly naming it in your push options, for example git push origin master:master.Pavo
I am wondering in which case "push.default=upstream" might be wrong. Kyralessa pointed out one example above, but this is an example which might not even be "wrong". So I am wondering why not always to use "push.default=upstream"; what could be wrong with that ?Melatonin
When matching is used, git push says Everything up-to-date if upstream is set to . or when branch of the same name does not exist in the remote. To push the branch into the remote repository, you have to use git push <remote> <branch>, e.g. git push origin testing.Nutwood
It's worth noting that as of Git 2.0 the simple behaviour is now the default.Melmela
Default behaviour should be rather nothing, than simple. Don't let beginners make any mistakes,... (applies also to advanced users, which might overlook something,...)Mani
There is a lot worth notingSmalto
git config --global push.default didn't show anything for me.Feverwort
also useful to know, regardless of push.default you can push all matching branches with eg, git push origin :Shotputter
11 year old answer changed my life.Selfimmolation
Has nothing been worth noting since 2015?Enterpriser
@Enterpriser "Useful to know" is just another way to say "worth noting" :)Recombination
H
243

You can set up default behavior for your git with push.default

git config push.default current

or if you have many repositories and want the same for all then

git config --global push.default current

The current in this setup means that by default you will only push the current branch when you do git push

Other options are:

  • nothing : Do not push anything
  • matching : Push all matching branches (default)
  • tracking : Push the current branch to whatever it is tracking
  • current : Push the current branch

UPDATE - NEW WAY TO DO THIS

As of Git 1.7.11 do the following:

git config --global push.default simple

This is a new setting introduced that works in the same way as current, and will be made default to git from v 2.0 according to rumors

Handspike answered 6/10, 2011 at 10:45 Comment(3)
Yes I read the answer you are referring to, but that answer only tells what to do and not how to do it. So I added my answer so all the info needed to set it up is on the same page.Handspike
OK; it's better to suggest an edit to the said post, because nobody will see your answer, as it's not likely to get as many votesDepreciable
how would one go about pulling to current branch? git pull origin?Pompey
T
210

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

Tommyetommyrot answered 4/6, 2009 at 3:16 Comment(6)
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.Donau
Probably I'm running 1.6.3.1. I did find it on the site I linked however.Tommyetommyrot
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.Donau
@Donau 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.Tommyetommyrot
This question is old but for anybody new, @docgnome is right. Just running 'git push origin' will push all of the branches instead of only the current branch. Use 'git push -f -v -n origin development' to force push a branch named development. Use the -n flag to simulate the git push result so you can see in advance which branch(es) will be affected. If it looks good then run 'git push -f -v origin development'. This might be useful https://mcmap.net/q/13252/-git-push-f-vsSpoonful
This answer was correct a long, long time ago, but is misleading now. Almost no users will have an old enough git version to default to push.default=matching behavior.Exclosure
S
28

Here is a very handy and helpful information about Git Push: Git Push: Just the Tip

The most common use of git push is to push your local changes to your public upstream repository. Assuming that the upstream is a remote named "origin" (the default remote name if your repository is a clone) and the branch to be updated to/from is named "master" (the default branch name), this is done with: git push origin master

git push origin will push changes from all local branches to matching branches the origin remote.

git push origin master will push changes from the local master branch to the remote master branch.

git push origin master:staging will push changes from the local master branch to the remote staging branch if it exists.

Supremacist answered 9/8, 2012 at 8:2 Comment(2)
git push origin branch_name for some reason push not only branch_name branch, but also other my local branches (git version 1.9.1).Operand
git push origin master:staging is an awesome hidden gem!Draggletailed
I
21

You can push current branch with command

git push origin HEAD

(took from here)

Inconvenient answered 23/6, 2015 at 7:50 Comment(1)
this seems to be a good way to avoid retying long branch names to be able to push them to the remote the first time. HEAD is not too long to type and you don't change the default behaviour of git +1Abominate
N
19

(March 2012)
Beware: that default "matching" policy might change soon
(sometimes 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: [email protected]

Nissensohn answered 17/3, 2012 at 11:13 Comment(0)
D
18

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

pub = "!f() { git push -u ${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.

Dyaus answered 15/3, 2010 at 0:50 Comment(2)
That is nice, but it unfortunately assumes that the branch has the same name on the other repository. Try git push -u --repo="origin" $1; instead. It works quite well, except if you push to another repository, the branch name will be the name used by the other repository, not the one you are pushing fromGlossitis
Hey thanks! Makes me wanna do a more complete version that checks the tracking status before pushing. But I'll stick with mine for now since I rarely have different branch names between repos.Dyaus
M
12

You can change that default behavior in your .gitconfig, for example:

[push]
  default = current

To check the current settings, run:

git config --global --get push.default
Midship answered 24/10, 2015 at 17:10 Comment(0)
E
9

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.

Exhibitive answered 4/6, 2009 at 2:49 Comment(3)
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.Tamberg
This is not necessary if you look at the above post by "Brian L".Waftage
It is, as there is no equv. for pull pull.defaultSeng
N
3

Rather than using aliases, I prefer creating git-XXX scripts so I can source control them more easily (our devs all have a certain source controlled dir on their path for this type of thing).

This script (called git-setpush) will set the config value for remote.origin.push value to something that will only push the current branch:

#!/bin/bash -eu

CURRENT_BRANCH=$(git branch | grep '^\*' | cut -d" " -f2)
NEW_PUSH_REF=HEAD:refs/for/$CURRENT_BRANCH

echo "setting remote.origin.push to $NEW_PUSH_REF"
git config remote.origin.push $NEW_PUSH_REF

note, as we're using Gerrit, it sets the target to refs/for/XXX to push into a review branch. It also assumes origin is your remote name.

Invoke it after checking out a branch with

git checkout your-branch
git setpush

It could obviously be adapted to also do the checkout, but I like scripts to do one thing and do it well

Neoplasty answered 19/4, 2012 at 15:55 Comment(2)
great idea setting remote.origin.push for gerrit usage. My local feature branches feature/fix_fubar are all pointed to more generic upstream branches like master or develop, so this would point at the wrong upstream. What's your local flow look like for gerrit controlled repos?Wilhoit
If you only have one "target" branch on gerrit, try simply git config remote.origin.push HEAD:refs/for/master.Telpher
F
3

New config in git 2.37.0

Run to set auto setup remote instead of changing the push default behavior

git config --global --add --bool push.autoSetupRemote true

it works well with push.default is to simple, upstream

References: answer tweet docs commit

Filomena answered 17/7, 2022 at 18:54 Comment(0)
P
2

I have added the following functions into my .bashrc file to automate these tasks. It does git push/git pull + name of current branch.

function gpush()
{
  if [[ "x$1" == "x-h" ]]; then
    cat <<EOF
Usage: gpush
git: for current branch: push changes to remote branch;
EOF
  else
    set -x
    local bname=`git rev-parse --abbrev-ref --symbolic-full-name @{u} | sed -e "s#/# #"`
    git push ${bname}
    set +x
  fi
}

function gpull()
{
  if [[ "x$1" == "x-h" ]]; then
    cat <<EOF
Usage: gpull
git: for current branch: pull changes from
EOF
  else
    set -x
    local bname=`git rev-parse --abbrev-ref --symbolic-full-name @{u} | sed -e "s#/# #"`
    git pull ${bname}
    set +x
  fi
}
Petey answered 22/5, 2014 at 7:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.