When I create a new repo on my gitolite repository I always have to enter the following command before I can start pushing code to the server.
git push origin master:refs/heads/master
What does it do ?
My guess is that is has to do with the head reference not sure. Could someone explain it to me?
master:refs/heads/master
is a refspec.refspecs
are of the form+<src>:<dst>
So here master is the ref on your local repo that you are pushing to the
refs/heads/master
refspec on the remote (origin).master
is short forrefs/heads/master
actually.In fact, you can just do
git push origin master
whereby it will push master on your local to master on remote. Only when you want to push to a different ref do you need to explicitly specify the destination ref.Also just
git push
has default behaviour too, which probably wouldn't have been the case before you did the first push and created a branch (master ) on the remote. So it would have seemed like you need to do the command you had mentioned. Refer to the manualIt sets up tracking for you. You can use the shorthand for this:
The part after the colon is the name of the branch on the remote repo. If you omit it, git assumes you want the same name.
Hope this helps.
The default behavior of
git push
, which is presumably what you describe as "pushing code to the server", is to only push local branches that have a matching branch, by name, on the remote you're pushing to. When you create a new repo, it has no branches in it, so a simplegit push
will push nothing. You have to explicitly push a branch by name first. Thereafter, the default behavior will work as you expect it to.P.S. Actually, you only have to
git push origin master
. What it does is push your local master to the gitolite repo as master, since you didn't specify a different name. If you saidgit push origin master:foo
, then the branch you locally call "master" would be known as "foo" on gitolite.P.P.S. You can switch default push behavior between "nothing", "matching" (the default), "trackings"/"upstream", and "current". See the settings for "push.default" on the git-config man page.
There's three parts to this command:
This invokes the push command
This names the remote to which you are pushing. This is either one of the named remotes stored in .git/config (you can list these with
git remote
), a URL, or the token.
which means the current repository.This is called a "refspec", and you can read about it in the man page for
git push
. But in general, it's comprised of two parts, separated by a colon. The first part is the name of a local branch, and the second part is the name of a branch on the remote repository (in this case,origin
). This particular refspec could be shortened tomaster:master
.In general, one can shorten refspecs even further. Just specifying
master
as the refspec is equivalent to using the same name on the remote, somaster
is the same asmaster:master
.