170

I found, in the official guide:

git push origin HEAD

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

However, the meaning of the command is not clear to me. Why does it have this effect?

I haven't been able to find an answer (this question seems to treat the problem, but the title is misleading).

2 Answers 2

206

HEAD points to the top of the current branch. git can obtain the branch name from that. So it's the same as:

git push origin CURRENT_BRANCH_NAME

but you don't have to remember/type the current branch name. Also it prevents you from pushing to the wrong remote branch by accident.

If you want to push a different branch than the current one the command will not work.

4
  • 22
    It might help to mention that HEAD is a symbolic ref, and it can be seen with git symbolic-ref HEAD. Commented Apr 23, 2014 at 11:23
  • 2
    What is the difference between: git push origin and git push origin HEAD?
    – Maciek
    Commented Oct 25, 2019 at 12:28
  • 1
    @MaciejD git push origin without specifying a branch name doesn't work. either you add a branch name or you say HEAD, meaning the current branch you're in Commented Dec 16, 2019 at 16:34
  • 7
    Actually git push origin is a valid command. If the push.default Git config setting is set to nothing then git push origin will "not push anything (error out)" according to man git-config. But other push.default settings have other behaviors. Search for push.default in man git-config. Also search for "When the command line does not specify where to push" and "When the command line does not specify what to push" in the description section at the top of man git-push (that's man git-push, not man git-config). Commented Mar 18, 2020 at 18:41
25

If you want to push into the specific remote branch you can run:

git push origin HEAD:<name-of-remote-branch>

This is what I encounter when I was trying to push my repo back to the remote branch.

Not the answer you're looking for? Browse other questions tagged or ask your own question.