Default behavior of “git push” without a branch specified

前端 未结 12 1180
眼角桃花
眼角桃花 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:17

    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 used to be the default, but not since Git 2.0 (simple is the new default).

    • 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) 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.

      This mode has become the default in Git 2.0.

    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 --global push.default
    

    To set a new configuration:

    git config --global push.default current
    
    0 讨论(0)
  • 2020-11-22 06:18

    You can push current branch with command

    git push origin HEAD
    

    (took from here)

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

    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

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

    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
    }
    
    0 讨论(0)
  • 2020-11-22 06:22

    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

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

    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.

    0 讨论(0)
提交回复
热议问题