Default behavior of “git push” without a branch specified

前端 未结 12 1179
眼角桃花
眼角桃花 2020-11-22 05:32

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

git push origin sandbox

If I say

git push origin

相关标签:
12条回答
  • 2020-11-22 06:00

    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

    0 讨论(0)
  • 2020-11-22 06:01

    I just committed my code to a branch and pushed it to github, like this:

    git branch SimonLowMemoryExperiments
    git checkout SimonLowMemoryExperiments
    git add .
    git commit -a -m "Lots of experimentation with identifying the memory problems"
    git push origin SimonLowMemoryExperiments
    
    0 讨论(0)
  • 2020-11-22 06:09

    (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:

    • http://thread.gmane.org/gmane.comp.version-control.git/123350/focus=123541
    • http://thread.gmane.org/gmane.comp.version-control.git/166743

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

    0 讨论(0)
  • 2020-11-22 06:12

    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.

    0 讨论(0)
  • 2020-11-22 06:13

    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.

    0 讨论(0)
  • 2020-11-22 06:14

    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
    
    0 讨论(0)
提交回复
热议问题