Git – push current branch shortcut

Is there a shortcut to tell Git to push the current tracking branch to origin?

Note: I know that I can change the default push behavior, but I am looking for an ad-hoc solution that does not change the default behavior.

For example, suppose I am on branch feature/123-sandbox-tests I would be using

  • I want make push and get error: src refspec master does not match any
  • Why are there .sln, .suo and .csproj files?
  • Git - Cannot delete remote branch
  • What is the difference between git am and git apply?
  • Ubuntu / DigitalOcean - Unable to fetch packages to install git on a fresh install
  • How to commit on master branch DB related code?
  • git push origin feature/123-sandbox-tests
    

    which is tedious. I am looking for a shortcut, something like

    git push origin current
    

    where git knows that current is feature/123-sandbox-tests.


    Edit: Starting from version 2.0, git’s default behavior has changed to a more intuitive behavior, which is what I wanted to achieve. See This SO question for details.

    Edit 2: ceztko’s answer is the best answer as it allows to push the current branch, regardless of the settings.

  • How to turn off git notifyCommit web hook trigger for individual Jenkins jobs?
  • hash collisions between hg/mercurial and git?
  • Are git repositories copyable?
  • Using Git in a Company?
  • CLI git log statistics
  • “Unable to determine upstream SVN information from working tree history”
  • 8 Solutions collect form web for “Git – push current branch shortcut”

    According to git push documentation:

    git push origin HEAD
        A handy way to push the current branch to the same name on the remote.
    

    So I think what you need is git push origin HEAD. Also it can be useful git push -u origin HEAD to set upstream tracking information in the local branch, if you haven’t already pushed to the origin.

    You can configure git to push to the current branch using the following command

    git config --global push.default current
    

    then just do

    git push 
    

    this will push the code to your current branch.

    You should take a look to a similar question in Default behavior of "git push" without a branch specified

    Basically it explains how to set the default behavior to push your current branch just executing git push. Probably what you need is:

    git config --global push.default current

    Other options:

    • nothing : Do not push anything
    • matching : Push all matching branches
    • upstream/tracking : Push the current branch to whatever it is tracking
    • current : Push the current branch

    The simplest way: run git push -u origin feature/123-sandbox-tests once. That pushes the branch the way you’re used to doing it and also sets the upstream tracking info in your local config. After that, you can just git push to push tracked branches to their upstream remote(s).

    You can also do this in the config yourself by setting branch.<branch name>.merge to the remote branch name (in your case the same as the local name) and optionally, branch.<branch name>.remote to the name of the remote you want to push to (defaults to origin). If you look in your config, there’s most likely already one of these set for master, so you can follow that example.

    Finally, make sure you consider the push.default setting. It defaults to “matching”, which can have undesired and unexpected results. Most people I know find “upstream” more intuitive, which pushes only the current branch.

    Details on each of these settings can be found in the git-config man page.

    On second thought, on re-reading your question, I think you know all this. I think what you’re actually looking for doesn’t exist. How about a bash function something like (untested):

    function pushCurrent {
      git config push.default upstream
      git push
      git config push.default matching
    }
    

    If you are using git 1.7.x, you can run the following command to set the remote tracking branch.

    git branch --set-upstream feature/123-sandbox-tests origin/feature/123-sandbox-tests
    

    Then you can simply use git push to push all the changes. For a more complete answer, please see the accepted answer to a similar question here.

    If you only want to push the current branch with the push command, then you can change the push behaviour to upstream:

    git config --global push.default upstream
    

    For what it’s worth, the ultimate shortcut:

    In my .bash_profile I have alias push="git push origin HEAD", so whenever i type push I know I’m pushing to the current branch I’m on.

    I use such alias in my .bashrc config

    alias gpb='git push origin `git rev-parse --abbrev-ref HEAD`'
    

    On the command $gpb it takes the current branch name and pushes it to the origin.

    Here are my other aliases:

    alias gst='git status'
    alias gbr='git branch'
    alias gca='git commit -am'
    alias gco='git checkout'
    

    With the help of ceztko’s answer I wrote this little helper function to make my life easier:

    function gpu()
    {
        if git rev-parse --abbrev-ref --symbolic-full-name @{u} > /dev/null 2>&1; then
            git push origin HEAD
        else
            git push -u origin HEAD
        fi
    }
    

    It pushes the current branch to origin and also sets the remote tracking branch if it hasn’t been setup yet.

    Git Baby is a git and github fan, let's start git clone.