Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating branches on an empty project in git

Let's say I am about to implement 3 different features in 3 different files (fileA, fileB an fileC) for a new project of mine.

I thought I'd just need to add my (currently empty) project to git:

git init 

and then create 3 different branches:

git branch file1_branch git branch file2_branch git branch file3_branch 

but this doesn't work:

fatal: Not a valid object name: 'master'.

Why is this?

Maybe the problem could be related that even the master branch wasn't created at this point? I tried doing a git branch. It yielded nothing.

I then thought about doing an "empty" commit to oblige git to create the master branch:

git commit -m `initial_commit` 

but as I didn't add any files to the staging area it doesn't work.

Keep in mind I'm just about to start my project, so at this point I don't have any files to add to the commit!

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

Thanks

like image 837
devoured elysium Avatar asked Apr 15 '11 15:04

devoured elysium


People also ask

How do I create an empty branch in git?

new empty git branch.md$ git checkout --orphan NEWBRANCH $ git rm -rf . --orphan creates a new branch, but it starts without any commit. After running the above command you are on a new branch "NEWBRANCH", and the first commit you create from this state will start a new history without any ancestry.

How do I create a branch for an existing project?

Creating a branch from a previous commit Click History. Right-click on the commit you would like to create a new branch from and select Create Branch from Commit. Under Name, type the name of the new branch.

How do you create branches in git?

New BranchesThe git branch command can be used to create a new branch. When you want to start a new feature, you create a new branch off main using git branch new_branch . Once created you can then use git checkout new_branch to switch to that branch.


1 Answers

Git represents branches as pointers to the latest commit in that branch. If you haven't created a commit yet, there's nothing for that branch to point to. So you can't really create branches until you have at least one commit.

Git represents commits as reference to one or more parent commits (or an all-zeros parent if this is the initial commit), a commit message and some other metadata, and a tree reference. A tree is basically a manifest of what is in each file in each directory. If your directory is empty, there is nothing to put in the tree. Now, it is actually possible to create an empty commit as your first commit; you can run git commit --allow-empty -m "initial commit". But that is not really a common way of using Git.

In Git, you're expected not to use branches unless you need them. There's no need to create several branches until you have some content to branch. If you create three branches at the very beginning of development, will you have no common ancestry between them? In that case, why not just create three repos? Or are you going to be updating all three of them in sync until they start to diverge? That seems like a lot of extra work to keep all of them up to date; you should just wait to create them until they need to 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.

like image 197
Brian Campbell Avatar answered Oct 12 '22 04:10

Brian Campbell