Creating branches in an empty project in git

Let's say I'm going to implement 3 different functions in 3 different files (fileA, fileB fileC) for my new project.

I thought I just needed to add my (currently empty) project in git:

git init 

and then create 3 different branches:

 git branch file1_branch git branch file2_branch git branch file3_branch 

but this does not work:

fatal: Invalid object name: 'master'.

Why is this?

Maybe the problem may be due to the fact that even the master branch has not been created at the moment? I tried to make a git branch . It didn’t give anything.

Then I thought about making an “empty” commit to force git to create a master branch:

 git commit -m `initial_commit` 

but since I did not add any files to the staging area, it does not work.

Keep in mind that I am going to start my project, so at the moment I have no files to add to commit!

How to solve this problem? Am I doing something wrong?

thank

+44
git version-control
Apr 15 '11 at 15:05
source share
2 answers

Git presents branches as pointers to the last commit in this branch. If you have not already created a commit, nothing is needed for this branch. Thus, you cannot create branches until you get at least one commit.

Git is a commit as a reference to one or more parent commits (or the parent of all zeros if this is the initial commit), a commit message and some other metadata, and a tree reference. The tree is basically a manifesto of what is in every file in every directory. If your directory is empty, there is nothing to put in the tree. Now you can actually create an empty commit as your first commit; you can run git commit --allow-empty -m "initial commit" . But this is not the usual way to use Git.

In Git, you expect that you will not use branches if you do not need them. There is no need to create multiple branches until you have any content for branching. If you create three branches at the very beginning of development, you have no common origin between them? In that case, why not just create three repositories? Or are you going to update all three of them synchronously until they start to diverge? It seems like a lot of work to keep them updated on the latest developments; you just have to wait to create them until they diverge.

But if you want to use this workflow, you can run git commit --allow-empty -m "initial commit" to create an empty initial commit.

+72
Apr 15 2018-11-11T00:
source share

You can make an empty initial commit with:

 git commit --allow-empty 

And then you can create your own branches as you wish.

+13
Apr 15 2018-11-11T00:
source share



All Articles