threads / discuss / 30205

Why can't git open empty branches ?

Subject: Why can't git open empty branches ?

## tl;dr

8 messages between Apr 11, 2012 and Apr 11, 2012.

replies: 7people: 4as markdown or json

Hadmut Danisch· Apr 11, 2012, 10:35 UTC · lore
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· Apr 11, 2012, 10:59 UTC · re: Hadmut Danisch · lore

Re: Why can't git open empty branches ?

Hadmut Danisch wrote:
Show 6 quoted lines
> 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· Apr 11, 2012, 11:06 UTC · re: Jonathan Nieder · lore

Re: Why can't git open empty branches ?

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· Apr 11, 2012, 11:21 UTC · re: Hadmut Danisch · lore

Re: Why can't git open empty branches ?

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· Apr 11, 2012, 11:47 UTC · re: Matthieu Moy · lore

Re: Why can't git open empty branches ?

Am 11.04.2012 13:21, schrieb Matthieu Moy:
Show 7 quoted lines
> 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· Apr 11, 2012, 12:19 UTC · re: Hadmut Danisch · lore

Re: Why can't git open empty branches ?

Hadmut Danisch wrote:
Show 7 quoted lines
> 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· Apr 11, 2012, 12:19 UTC · re: Hadmut Danisch · lore

Re: Why can't git open empty branches ?

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· Apr 11, 2012, 14:16 UTC · re: Hadmut Danisch · lore

Re: Why can't git open empty branches ?

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.

← back to recent threads