# Why can't git open empty branches ?

8 messages from 2012-04-11 to 2012-04-11. Participants: Hadmut Danisch, Jonathan Nieder, Matthieu Moy, Holger Hellmuth.
Thread: https://gitlist.dev/t/30205

## Hadmut Danisch, 2012-04-11 10:35

Subject: Why can't git open empty branches ?
Message-ID: <4F855E6B.4010504@danisch.de>
URL: https://gitlist.dev/e/4F855E6B.4010504%40danisch.de

```
Hi,

when creating a new branch in git, it is by default filled with the
contents of the current branch.

Sometimes a new but empty branch is needed. There are plenty of
instructions in the web to achieve this with „dirty tricks”, i.e. fiddle
around with git's internals. Which is poor design.



Why doesn't git's branch command support opening empty branches in a
clean an natural way?



regards
Hadmut

```

## Jonathan Nieder, 2012-04-11 10:59

Subject: Re: Why can't git open empty branches ?
Message-ID: <20120411105906.GA19823@burratino>
URL: https://gitlist.dev/e/20120411105906.GA19823%40burratino
In-Reply-To: <4F855E6B.4010504@danisch.de>

```
Hadmut Danisch wrote:

> when creating a new branch in git, it is by default filled with the
> contents of the current branch.
>
> Sometimes a new but empty branch is needed. There are plenty of
> instructions in the web to achieve this with „dirty tricks”, i.e. fiddle
> around with git's internals.

	git init ../unrelated-topic
	cd ../unrelated-topic
	hack away...
	git commit
	cd -
	git fetch ../unrelated-topic master:<new branch>

Or:

	git checkout --orphan <new branch>
	git rm -fr .
	hack away...

Please feel free to contact the authors of the instructions you found
on the web to fix them.

Thanks and hope that helps,
Jonathan

```

## Hadmut Danisch, 2012-04-11 11:06

Subject: Re: Why can't git open empty branches ?
Message-ID: <4F8565BC.1070701@danisch.de>
URL: https://gitlist.dev/e/4F8565BC.1070701%40danisch.de
In-Reply-To: <20120411105906.GA19823@burratino>

```
Am 11.04.2012 12:59, schrieb Jonathan Nieder:
> 	git checkout --orphan <new branch>
> 	git rm -fr .



I would have expected something more obvious and intuitive like

  git branch --empty



It is not really plausible to create a new branch by checking out a
non-existing one.

However, this helps. Thanks.

```

## Matthieu Moy, 2012-04-11 11:21

Subject: Re: Why can't git open empty branches ?
Message-ID: <vpq7gxmzf75.fsf@bauges.imag.fr>
URL: https://gitlist.dev/e/vpq7gxmzf75.fsf%40bauges.imag.fr
In-Reply-To: <4F8565BC.1070701@danisch.de>

```
Hadmut Danisch <hadmut@danisch.de> writes:

> It is not really plausible to create a new branch by checking out a
> non-existing one.

... but it is hardly useful to create a new branch unless you want to
check it out.

I think the most common way to create a branch with Git is "git checkout
-b new-branch", which does the creation+checkout in a single command.
"git checkout --orphan" is the natural extension of it.

BTW, it is rarely good practice to create an empty branch in an existing
repository. You'll have different branches that do not share any
history, and they would likely be better in separate repositories (or at
least, be in separate local repositories, pushed to the same remote bare
repository, in which case you don't need anything special, just "git
init" and "git push"). That doesn't mean you shouldn't do it, but just
that you may want to think twice before doing it ;-).

-- 
Matthieu Moy
http://www-verimag.imag.fr/~moy/

```

## Hadmut Danisch, 2012-04-11 11:47

Subject: Re: Why can't git open empty branches ?
Message-ID: <4F856F67.4000609@danisch.de>
URL: https://gitlist.dev/e/4F856F67.4000609%40danisch.de
In-Reply-To: <vpq7gxmzf75.fsf@bauges.imag.fr>

```
Am 11.04.2012 13:21, schrieb Matthieu Moy:
> BTW, it is rarely good practice to create an empty branch in an existing
> repository. You'll have different branches that do not share any
> history, and they would likely be better in separate repositories (or at
> least, be in separate local repositories, pushed to the same remote bare
> repository, in which case you don't need anything special, just "git
> init" and "git push"). That doesn't mean you shouldn't do it, but just
> that you may want to think twice before doing it ;-).

That's a pretty good point for discussion.


Sometimes people are working on different sorts of information, that are
nevertheless closely related, e.g. open source software and the web
pages describing it (like in git hub), or a web server tree and the
software generating it. They are related, but do not logically share a
history.


Creating independent branches by pushing two separates into a single
remote bare is a nice idea, but if I understood git correctly, the very
first commit in a repos is always to the master branch, where you have
two masters trying to push into the shared remote bare. This is
obviously solvable if you use the correct commands and maybe delete and
re-clone the repos, but this is all overcomplicated and non-trivial.
Nothing I could do without reading manuals.

So your proposal might work perfectly, but in my eyes it is error prone
and not user friendly.

regards

```

## Jonathan Nieder, 2012-04-11 12:19

Subject: Re: Why can't git open empty branches ?
Message-ID: <20120411121907.GA30340@burratino>
URL: https://gitlist.dev/e/20120411121907.GA30340%40burratino
In-Reply-To: <4F856F67.4000609@danisch.de>

```
Hadmut Danisch wrote:

> Creating independent branches by pushing two separates into a single
> remote bare is a nice idea, but if I understood git correctly, the very
> first commit in a repos is always to the master branch, where you have
> two masters trying to push into the shared remote bare. This is
> obviously solvable if you use the correct commands and maybe delete and
> re-clone the repos, but this is all overcomplicated and non-trivial.
> Nothing I could do without reading manuals.

I suppose you had to read the manual to learn the "git commit" and
"git branch -m" commands, yes.  But what's wrong with that, or how
could we fix it?

	git init code
	cd code
	... hack away ...
	git remote add origin <url>
	git push -u origin master
	cd ..

	git init website
	cd website
	... hack away ...
	git branch -m website
	git remote add origin <url>
	git push -u origin website
	cd ..

I wonder if there is a potential documentation or error message update
lurking behind these questions.

Hope that helps,
Jonathan

```

## Matthieu Moy, 2012-04-11 12:19

Subject: Re: Why can't git open empty branches ?
Message-ID: <vpqbomyv4t3.fsf@bauges.imag.fr>
URL: https://gitlist.dev/e/vpqbomyv4t3.fsf%40bauges.imag.fr
In-Reply-To: <4F856F67.4000609@danisch.de>

```
Hadmut Danisch <hadmut@danisch.de> writes:

> e.g. open source software and the web pages describing it (like in git
> hub), or a web server tree and the software generating it. They are
> related, but do not logically share a history.

In these cases, you'll want to have two working directories, if only to
avoid:

git checkout documentation
# fix a typo
git commit
git checkout code
make
# complete rebuild triggered.

> Creating independent branches by pushing two separates into a single
> remote bare is a nice idea, but if I understood git correctly, the very
> first commit in a repos is always to the master branch,

Not necessarily. I don't see an option in "git init" to change the
default branch name, but you can

git init foo
cd foo
git checkout -b whatever-branch-name

and work.

Otherwise, you can have a branch that is locally called "master", and
push it as another name (but that is probably the "error prone" in your
message ;-) ).

-- 
Matthieu Moy
http://www-verimag.imag.fr/~moy/

```

## Holger Hellmuth, 2012-04-11 14:16

Subject: Re: Why can't git open empty branches ?
Message-ID: <4F859245.908@ira.uka.de>
URL: https://gitlist.dev/e/4F859245.908%40ira.uka.de
In-Reply-To: <4F856F67.4000609@danisch.de>

```
Hi Hadmut ;-)

On 11.04.2012 13:47, Hadmut Danisch wrote:
> Creating independent branches by pushing two separates into a single
> remote bare is a nice idea, but if I understood git correctly, the very
> first commit in a repos is always to the master branch, where you have

AFAIK the only thing that makes master the initial branch is the file 
.git/HEAD that points to master after you "git init". This is not hardcoded.

 > So your proposal might work perfectly, but in my eyes it is error
 > prone and not user friendly.

Serious errors should not happen since the bare repo on the server is 
created with the flag denyNonFastForwards. So even if you had a master 
branch in both repos, only one of them, the first one, would succeed in 
pushing to the server, a push of the second one would give you an error 
message. Maybe not user friendly, but safe.

```
