Git - push current branch shortcut

Git

Git Problem Overview


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.

Git Solutions


Solution 1 - Git

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.

Solution 2 - Git

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.

Solution 3 - Git

You should take a look to a similar question in https://stackoverflow.com/questions/948354/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

Solution 4 - Git

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'

Solution 5 - Git

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.

Solution 6 - Git

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

Solution 7 - Git

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
}

Solution 8 - Git

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionEladView Question on Stackoverflow
Solution 1 - GitceztkoView Answer on Stackoverflow
Solution 2 - GitMaheshView Answer on Stackoverflow
Solution 3 - GitNajorView Answer on Stackoverflow
Solution 4 - GitfedorshishiView Answer on Stackoverflow
Solution 5 - GitRyan ReboView Answer on Stackoverflow
Solution 6 - GitFaruk SahinView Answer on Stackoverflow
Solution 7 - GitRyan StewartView Answer on Stackoverflow
Solution 8 - GitBantakView Answer on Stackoverflow