445

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

git push origin feature/123-sandbox-tests

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

git push origin current # <- example, not working

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.

Elad
  • 15,967
  • 17
  • 58
  • 69

8 Answers8

840

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.

romellem
  • 3,144
  • 1
  • 22
  • 50
ceztko
  • 13,391
  • 2
  • 44
  • 64
  • 14
    I prefer this over setting push.default to simple or current, because different machines could have different settings. You may get used to `git push` only pushing the current branch, but on other machines, you may accidentally push all matching branches. – wisbucky Sep 08 '14 at 02:05
  • 1
    @wisbucky I have several aliases, including `co`, so if I were to try to even pull down and then check out code without my `~/.gitconfig` file on that VM, I'll know it immediately. That lets me feel pretty safe about changing the push default to `upstream`. – Damon May 19 '15 at 17:36
  • 13
    Interesting that this doesn't work with just `@`, which is documented as a valid alias to `HEAD`. (e.g. `git push -u origin @`) – void.pointer Jun 16 '15 at 13:45
  • The answer at https://stackoverflow.com/a/30706402/850996 is a more correct answer for the question in the OP – Shyam Habarakada Oct 28 '20 at 21:22
  • 1
    @ShyamHabarakada well, the OP edited the question stating "ceztko's answer is the best answer as it allows to push the current branch, **regardless of the settings**" – ceztko Oct 29 '20 at 07:59
248

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.

Mahesh
  • 6,186
  • 2
  • 23
  • 34
  • Does it make sense to add `push = refs/heads/current:refs/for/master/current` to a 'remote' section in .git/config ? That is, is 'current' a special word here? Or will it be looking for a branch called "current"? – David Doria Feb 09 '16 at 14:18
  • 6
    @DavidDoria 'current' is not a branch named here. – Mahesh Feb 11 '16 at 08:21
  • 1
    Best do `git push -u origin feature_branch_name` to set up upstream/tracking! If you use a remote repository at least. – Flo Apr 26 '20 at 17:50
  • I can't imagine a workflow in which "push master even when I'm not on master" is a sane default. I guess if master isn't an important branch and you're fine with whatever untested, un-reviewed, probably-insecure garbage ending up on it, then I guess these defaults aren't *harmful*, but for any other workflow this default seems dangerous. – weberc2 Feb 08 '21 at 17:40
46

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
Community
  • 1
  • 1
Najor
  • 619
  • 5
  • 11
13

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'
theodor
  • 1,371
  • 1
  • 14
  • 32
9

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.

Ryan Rebo
  • 1,168
  • 1
  • 10
  • 22
6

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
Community
  • 1
  • 1
Faruk Sahin
  • 7,418
  • 5
  • 25
  • 31
  • Thanks, however in this case wouldn't ALL tracking branches be pushed to origin? – Elad Dec 25 '12 at 15:45
  • I have modified the answer, please see the link, the answer is more complete there. – Faruk Sahin Dec 25 '12 at 15:51
  • Again, the problem is that all branches the are tracking branches are being pushed that way. As I wrote in my question the branch is a tracked branch in the first place, so I think that setting the upstream branch as you suggested is redundant. Or am I wrong? – Elad Dec 26 '12 at 07:51
  • Then you can configure git to push only the current branch by changing the git push behaviour. `git config --global push.default upstream` will set the push behaviour to push only the current branch. – Faruk Sahin Dec 26 '12 at 10:17
5

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
}
Ryan Stewart
  • 115,853
  • 19
  • 167
  • 192
1

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.

Bantak
  • 508
  • 4
  • 11