466

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.

8 Answers 8

866

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.

Sign up to request clarification or add additional context in comments.

5 Comments

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 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.
Interesting that this doesn't work with just @, which is documented as a valid alias to HEAD. (e.g. git push -u origin @)
The answer at stackoverflow.com/a/30706402/850996 is a more correct answer for the question in the OP
@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"
260

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.

4 Comments

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"?
@DavidDoria 'current' is not a branch named here.
Best do git push -u origin feature_branch_name to set up upstream/tracking! If you use a remote repository at least.
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.
47

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

Comments

14

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' 

Comments

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.

Comments

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 

4 Comments

Thanks, however in this case wouldn't ALL tracking branches be pushed to origin?
I have modified the answer, please see the link, the answer is more complete there.
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?
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.
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 } 

Comments

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.