Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git repository created without a master branch

Tags:

I want to create a new shared git repository for a new project (on a VM).

I ran git --bare init from /.../git/new_proj.git, but a master branch is not created in the .../git/new_proj.git/refs/heads directory. I also ran sudo chmod 777 -R on my directory, but it didn't help, and still no master is created after the init command.

Edit: I even tried to use git init (without the bare flag), but still the master branch was not created.

Google wasn't much help in this matter...

Anyone know what the problem is? Something I'm missing? Thanks!

like image 636
Ayelet Avatar asked Jan 21 '14 08:01

Ayelet


People also ask

Can we have git without master branch?

Master and branch makes no difference for git. So you can use git without a master.

Why there is no master branch in git?

The remote you cloned from might still have a master branch (you could check with git ls-remote origin master ), but you wouldn't have created a local version of that branch by default, because git clone only checks out the remote's HEAD .

Is master branch automatically created?

Note that, the master branch is the default branch in Git. It is automatically created and used when you initialize a new Git repository. In the next sections of this article below, I am going to show you how to create Git branches, use Git branches and remove Git branches. So, let's move forward.


2 Answers

It's already in the comments on the other answer so far, but this is perfectly normal: a new repository, "bare" or not, has no commits, so it has no references either.

It does have a HEAD, which is a file named HEAD in the repository (the .git directory), containing a symbolic branch name reference. If you cat HEAD you'll see ref: refs/heads/master, meaning that the new repository is "on branch master", even though branch master does not yet exist. Again, this is a perfectly normal state of affairs. You're said to be "on an unborn branch", at this point.

When you add one or more commits to this empty repository, the master branch can—if it's a bare repo, you may be adding this via git push which may not provide a master branch, so let's not say "does" :-) although usually master does—come into existence at that point, with the reference pointing to the new commit (or the new tip commit of a chain of commits).

In any repo (bare or not, again), you can be "on" a branch that does not exist. In particular, in an ordinary repo, you can do:

$ git checkout --orphan newbranch 

which puts you "on" newbranch (by writing ref: refs/heads/newbranch into HEAD) without actually creating newbranch yet, making newbranch an "unborn branch". The next commit then causes newbranch to come into existence, and that commit has no parent commits (hence the --orphan part): it is a new root commit. This is the same way master comes into existence on its first commit.

If you like, you can look at it in terms of underlying mechanism: when git creates a new commit, the steps used to update HEAD go like this:1

  1. Read contents of HEAD file.
  2. Is it a symbolic ref such as ref: refs/heads/master? if yes, go to step 4.
  3. No ("detached HEAD" case): create commit with parent given by commit ID in HEAD and write new SHA-1 into HEAD. Stop, we are done.
  4. Read SHA-1 of referred-to branch (e.g., .git/refs/heads/master, or from packed refs).
  5. If no SHA-1 is available because branch does not exist yet, create root commit, else create commit whose parent is the given SHA-1. Write new SHA-1 to referred-to branch. Stop, we are done.

As an interesting side note, when refs get packed, "active" ones (like whichever branch you're developing on, let's say devel for instance) wind up in .git/packed-refs, but are quickly updated with new values. Those new values go only in the .git/refs/heads/devel file: the .git/packed-refs file retains a refs/heads/devel entry, but it's stale (and hence ignored). (You're not supposed to depend on this: external programs, such as shell scripts, should use git branch or git update-ref or git symbolic-ref as appropriate, to read and write ref-names and SHA-1 values. Occasionally, though, it's useful to be able to go in and edit refs directly. Think of it as a modern-day equivalent of a hex editor for disk sectors. :-) )


1This all assumes you are not creating a merge commit. If you are in the middle of a merge, another file in the repository (.git/MERGE_HEAD) supplies the extra merge parent IDs.

like image 94
torek Avatar answered Sep 26 '22 17:09

torek


i have same issue and fixed - first of all use git add command after that use commit command finally use git branch to show your branched my git version 1.9

git add -A git commit -m "initialize" git branch 
like image 21
Bafi Avatar answered Sep 23 '22 17:09

Bafi