VortixDev
VortixDev

Reputation: 1013

What is the difference between "git push remote branch" and "git push remote HEAD:branch"?

Trying the former, I got error: src refspec branch does not match any., whilst trying the latter succeeded.

In case it's noteworthy, the branch name was 1.3.

What's the difference between these two, and why did the first fail when the second succeeded?

Upvotes: 1

Views: 361

Answers (2)

Greg Bacon
Greg Bacon

Reputation: 139711

Based on the error you saw, your local repository does not seem to have a branch named branch, but the remote repository must have had one already, for example if a collaborator already pushed to branch.

The particular argument to git push in question is known as the refspec, and with a colon separator, you can in effect rename branches or objects on the remote side. Therefore, a refspec of HEAD:branch means push the branch beginning at the most recent commit on the current branch if the HEAD symbolic reference refers to a branch (or the commit that HEAD refers to if detached) but to the ref branch on the remote side.

For completeness, if branch does not yet exist on the remote, you must invoke

git push remote HEAD:refs/heads/branch

<refspec>…​

Specify what destination ref to update with what source object. The format of a <refspec> parameter is an optional plus +, followed by the source object <src>, followed by a colon :, followed by the destination ref <dst>.

The is often the name of the branch you would want to push, but it can be any arbitrary "SHA-1 expression", such as master~4 or HEAD (see gitrevisions).

The tells which ref on the remote side is updated with this push. Arbitrary expressions cannot be used here, an actual ref must be named. If git push [<repository>] without any <refspec> argument is set to update some ref at the destination with <src> with remote.<repository>.push configuration variable, :<dst> part can be omitted—​such a push will update a ref that <src> normally updates without any <refspec> on the command line. Otherwise, missing :<dst> means to update the same ref as the <src>.

Upvotes: 1

eftshift0
eftshift0

Reputation: 30317

That when you say HEAD:branch you don't need to have a local branch with the same name of the remote branch on your local... when you type git push some-remote HEAD:some-branch you are asking it to put whatever you have checked out at the moment as the remote branch, regardless of what it is called on your local (or working on detached HEAD).

Upvotes: 1

Related Questions