# git branch --switch?

18 messages from 2007-04-17 to 2007-04-17. Participants: Rene Herman, Alex Riesen, Brian Gernhardt, Lars Hjemli, Linus Torvalds, Randal L. Schwartz.
Thread: https://gitlist.dev/t/7717

## Rene Herman, 2007-04-17 13:36

Subject: git branch --switch?
Message-ID: <4624CD58.90103@gmail.com>
URL: https://gitlist.dev/e/4624CD58.90103%40gmail.com

```
Good day.

Is it possible to switch the current branch without checking it out? Not 
really essential, but I'm happily flaundering around with git and still 
start from scratch fairly regularly; to speed this up I've found the -n 
switch to git clone useful and would like something similar when 
reconstructing my "branch hierarchies".

Upto now I only know about "git checkout" (with or without -b) to switch the 
current branch. As said it's not really essential, but I was expecting there 
would be something like a "branch --switch". Did I overlook it?

Rene.

```

## Alex Riesen, 2007-04-17 14:21

Subject: Re: git branch --switch?
Message-ID: <81b0412b0704170721i2b64999aqbd0d7489074454d6@mail.gmail.com>
URL: https://gitlist.dev/e/81b0412b0704170721i2b64999aqbd0d7489074454d6%40mail.gmail.com
In-Reply-To: <4624CD58.90103@gmail.com>

```
On 4/17/07, Rene Herman <rene.herman@gmail.com> wrote:
> Is it possible to switch the current branch without checking it out? Not
> really essential, but I'm happily flaundering around with git and still
> start from scratch fairly regularly; to speed this up I've found the -n
> switch to git clone useful and would like something similar when
> reconstructing my "branch hierarchies".
>
> Upto now I only know about "git checkout" (with or without -b) to switch the
> current branch. As said it's not really essential, but I was expecting there
> would be something like a "branch --switch". Did I overlook it?

Kind of. It can be done with git-read-tree. I.e.:

  $ git-read-tree --index-output=.git/tmp-index <branch-name> && \
     mv tmp-index .git/index && \
     git update-ref HEAD <branch-name>

```

## Rene Herman, 2007-04-17 14:29

Subject: Re: git branch --switch?
Message-ID: <4624D9E0.7080303@gmail.com>
URL: https://gitlist.dev/e/4624D9E0.7080303%40gmail.com
In-Reply-To: <81b0412b0704170721i2b64999aqbd0d7489074454d6@mail.gmail.com>

```
On 04/17/2007 04:21 PM, Alex Riesen wrote:

> On 4/17/07, Rene Herman <rene.herman@gmail.com> wrote:

>> Is it possible to switch the current branch without checking it out?

[ ... ]

> Kind of. It can be done with git-read-tree. I.e.:
> 
>  $ git-read-tree --index-output=.git/tmp-index <branch-name> && \
>     mv tmp-index .git/index && \
>     git update-ref HEAD <branch-name>

Eew. Thank you. Maybe...

Rene.

```

## Brian Gernhardt, 2007-04-17 14:31

Subject: Re: git branch --switch?
Message-ID: <C940520E-732A-4F6E-A7F2-0215FD534455@silverinsanity.com>
URL: https://gitlist.dev/e/C940520E-732A-4F6E-A7F2-0215FD534455%40silverinsanity.com
In-Reply-To: <4624CD58.90103@gmail.com>

```

On Apr 17, 2007, at 9:36 AM, Rene Herman wrote:

> Is it possible to switch the current branch without checking it  
> out? Not really essential, but I'm happily flaundering around with  
> git and still start from scratch fairly regularly; to speed this up  
> I've found the -n switch to git clone useful and would like  
> something similar when reconstructing my "branch hierarchies".
>
> Upto now I only know about "git checkout" (with or without -b) to  
> switch the current branch. As said it's not really essential, but I  
> was expecting there would be something like a "branch --switch".  
> Did I overlook it?

Perusing git-checkout points me to git-symbolic-ref to update the  
HEAD ref to a new branch:

git symbolic-ref HEAD refs/heads/<branch>

However, I'm somewhat confused as to why you'd want HEAD and the  
working directory to get out of sync.  It would cause any further  
commits to have broken history information.  If you want to make the  
content of one branch ($branchA) the same as the other ($branchB),  
you should do something like:

git checkout -b temp $branchB    # Get content from branchB
git merge -s ours $branchA       # Merge history (not content) from  
branchA
git branch -M $branchA           # Make this new merge branchA

This would be significantly simpler if there was a "theirs" merge  
strategy "git checkout $branchA; git merge -s theirs $branchB", but  
there isn't.  Should be simple to write if you really need it though.

~~ Brian

```

## Brian Gernhardt, 2007-04-17 14:46

Subject: Re: git branch --switch?
Message-ID: <F47212AE-B31D-4025-9859-6794E7405DF2@silverinsanity.com>
URL: https://gitlist.dev/e/F47212AE-B31D-4025-9859-6794E7405DF2%40silverinsanity.com
In-Reply-To: <81b0412b0704170721i2b64999aqbd0d7489074454d6@mail.gmail.com>

```

On Apr 17, 2007, at 10:21 AM, Alex Riesen wrote:

> Kind of. It can be done with git-read-tree. I.e.:
>
>  $ git-read-tree --index-output=.git/tmp-index <branch-name> && \
>     mv tmp-index .git/index && \
>     git update-ref HEAD <branch-name>

This does not appear to do what you think it will.  git update-ref  
will write the SHA1 of <branch-name> into the current HEAD, not  
switch HEAD to a new branch.

However, the first two lines will correctly update the index to the  
new head which is probably a good idea.

~~ Brian

```

## Alex Riesen, 2007-04-17 15:11

Subject: Re: git branch --switch?
Message-ID: <81b0412b0704170811h510ce0afn1b2277b752bdf422@mail.gmail.com>
URL: https://gitlist.dev/e/81b0412b0704170811h510ce0afn1b2277b752bdf422%40mail.gmail.com
In-Reply-To: <F47212AE-B31D-4025-9859-6794E7405DF2@silverinsanity.com>

```
On 4/17/07, Brian Gernhardt <benji@silverinsanity.com> wrote:
>
> On Apr 17, 2007, at 10:21 AM, Alex Riesen wrote:
>
> > Kind of. It can be done with git-read-tree. I.e.:
> >
> >  $ git-read-tree --index-output=.git/tmp-index <branch-name> && \
> >     mv tmp-index .git/index && \
> >     git update-ref HEAD <branch-name>
>
> This does not appear to do what you think it will.  git update-ref
> will write the SHA1 of <branch-name> into the current HEAD, not
> switch HEAD to a new branch.
>

ach, right. Dangerous: it can change the ref HEAD points to. Very sorry!
Better just:

 $ rm .git/HEAD && git symbolic-ref HEAD "refs/heads/<branch-name>"

In the end, you just have git-checkout minus git-checkout-index. Maybe
a "git-checkout -n" can be useful. Even though I can't imagine what for.

```

## Rene Herman, 2007-04-17 15:41

Subject: Re: git branch --switch?
Message-ID: <4624EAAA.6040000@gmail.com>
URL: https://gitlist.dev/e/4624EAAA.6040000%40gmail.com
In-Reply-To: <C940520E-732A-4F6E-A7F2-0215FD534455@silverinsanity.com>

```
On 04/17/2007 04:31 PM, Brian Gernhardt wrote:

>> Is it possible to switch the current branch without checking it out? 
>> Not really essential, but I'm happily flaundering around with git and 
>> still start from scratch fairly regularly; to speed this up I've found 
>> the -n switch to git clone useful and would like something similar 
>> when reconstructing my "branch hierarchies".
>>
>> Upto now I only know about "git checkout" (with or without -b) to 
>> switch the current branch. As said it's not really essential, but I 
>> was expecting there would be something like a "branch --switch". Did I 
>> overlook it?
> 
> Perusing git-checkout points me to git-symbolic-ref to update the HEAD 
> ref to a new branch:
> 
> git symbolic-ref HEAD refs/heads/<branch>
> 
> However, I'm somewhat confused as to why you'd want HEAD and the working 
> directory to get out of sync.

Thank you for the answer. Well, as said, it's not essential, but I was just 
now rebuilding a repo and have a few branches that I all want to be based on 
the same revision. Say, branch a, b and c, based on v2.6.20.

git clone -l -s -n <a local linux repo> local
git checkout -b v20 v2.6.20
git branch a
git branch b
git branch c

Step 1, 3, 4 and 5 of this are nearly instantaneous but 2 isn't -- this repo 
sits on a P1 with 64M of memory and a disk doing 8 M/s which is probably the 
only reason I thought asking about it was a good idea in the first place...

You'd be quite right in saying that there isn't much point; if I want to now 
start populating branch a, I have to "git checkout a" anyway, and that 
action _will_ now be instantaneous. If I'd replaced 2 with:

git branch --create-and-set-as-current v20 v2.6.20

then I will not have won any time until that 6th "git checkout a" step.

The checkout of v20 was superfluous in this though, and I just expected I 
should be able to skip that. It fitted my mental model...

Rene.

```

## Rene Herman, 2007-04-17 15:44

Subject: Re: git branch --switch?
Message-ID: <4624EB76.2030700@gmail.com>
URL: https://gitlist.dev/e/4624EB76.2030700%40gmail.com
In-Reply-To: <81b0412b0704170811h510ce0afn1b2277b752bdf422@mail.gmail.com>

```
On 04/17/2007 05:11 PM, Alex Riesen wrote:

> ach, right. Dangerous: it can change the ref HEAD points to. Very sorry!
> Better just:
> 
> $ rm .git/HEAD && git symbolic-ref HEAD "refs/heads/<branch-name>"

Thanks.

> In the end, you just have git-checkout minus git-checkout-index. Maybe
> a "git-checkout -n" can be useful. Even though I can't imagine what for.

Thought about that as well, to keep with clone, but giving "checkout" an 
option to not checkout seems somewhat odd.

"git branch -s" for "not only create but Set current to it as well" maybe?

Rene.

```

## Rene Herman, 2007-04-17 15:47

Subject: Re: git branch --switch?
Message-ID: <4624EC18.4000500@gmail.com>
URL: https://gitlist.dev/e/4624EC18.4000500%40gmail.com
In-Reply-To: <4624EAAA.6040000@gmail.com>

```
On 04/17/2007 05:41 PM, Rene Herman wrote:

> On 04/17/2007 04:31 PM, Brian Gernhardt wrote:

>> However, I'm somewhat confused as to why you'd want HEAD and the 
>> working directory to get out of sync.
> 
> Thank you for the answer. Well, as said, it's not essential, but I was 
> just now rebuilding a repo and have a few branches that I all want to be 
> based on the same revision. Say, branch a, b and c, based on v2.6.20.
> 
> git clone -l -s -n <a local linux repo> local
> git checkout -b v20 v2.6.20
> git branch a
> git branch b
> git branch c
> 
> Step 1, 3, 4 and 5 of this are nearly instantaneous but 2 isn't -- this 
> repo sits on a P1 with 64M of memory and a disk doing 8 M/s which is 
> probably the only reason I thought asking about it was a good idea in 
> the first place...
> 
> You'd be quite right in saying that there isn't much point; if I want to 
> now start populating branch a, I have to "git checkout a" anyway, and 
> that action _will_ now be instantaneous. If I'd replaced 2 with:
> 
> git branch --create-and-set-as-current v20 v2.6.20
> 
> then I will not have won any time until that 6th "git checkout a" step.

s/until/after/

> The checkout of v20 was superfluous in this though, and I just expected 
> I should be able to skip that. It fitted my mental model...

Rene.

```

## Lars Hjemli, 2007-04-17 15:55

Subject: Re: git branch --switch?
Message-ID: <8c5c35580704170855ke426d86vac6e9b3b67fea5ea@mail.gmail.com>
URL: https://gitlist.dev/e/8c5c35580704170855ke426d86vac6e9b3b67fea5ea%40mail.gmail.com
In-Reply-To: <4624EAAA.6040000@gmail.com>

```
On 4/17/07, Rene Herman <rene.herman@gmail.com> wrote:
> git clone -l -s -n <a local linux repo> local
> git checkout -b v20 v2.6.20
> git branch a
> git branch b
> git branch c
>
> Step 1, 3, 4 and 5 of this are nearly instantaneous but 2 isn't

I might have misunderstood your goal, but have you tried

 git clone -l -s -n <a local linux repo> local
 git branch a v2.6.20
 git branch b a
 git branch c a

Now a, b and c all point at v2.6.20, while HEAD points as master.

--
larsh

```

## Rene Herman, 2007-04-17 16:10

Subject: Re: git branch --switch?
Message-ID: <4624F18F.6070906@gmail.com>
URL: https://gitlist.dev/e/4624F18F.6070906%40gmail.com
In-Reply-To: <8c5c35580704170855ke426d86vac6e9b3b67fea5ea@mail.gmail.com>

```
On 04/17/2007 05:55 PM, Lars Hjemli wrote:

> On 4/17/07, Rene Herman <rene.herman@gmail.com> wrote:

>> git clone -l -s -n <a local linux repo> local
>> git checkout -b v20 v2.6.20
>> git branch a
>> git branch b
>> git branch c
>>
>> Step 1, 3, 4 and 5 of this are nearly instantaneous but 2 isn't
> 
> I might have misunderstood your goal, but have you tried
> 
> git clone -l -s -n <a local linux repo> local
> git branch a v2.6.20
> git branch b a
> git branch c a
> 
> Now a, b and c all point at v2.6.20, while HEAD points as master.

Well, yes, they do, and I could also do

git branch b v2.6.20
git branch c v2.6.20

directly then (right?) but I do want that "v20" branch in the middle. The 
cloned repo is a linus repo, and that v20 is where I'll be pulling 2.6.20.y 
updates into; a merge branch will then merge v20, a, b and c into what I 
will be compiling.

As said, given that I need to checkout things anyway to do something with 
them it'a all not essential but as a newbie, I just thought that a "branch 
--and-set-as-current new_branch" made sense. My sense of sense may be crap; 
I've largely avoided dealing with souce code management, regarding CVS and 
SVN as unfortunate facts of life to work around and forget, mostly.

Rene.

```

## Rene Herman, 2007-04-17 16:24

Subject: Re: git branch --switch?
Message-ID: <4624F4BB.7010308@gmail.com>
URL: https://gitlist.dev/e/4624F4BB.7010308%40gmail.com
In-Reply-To: <4624EAAA.6040000@gmail.com>

```
On 04/17/2007 05:41 PM, Rene Herman wrote:

> You'd be quite right in saying that there isn't much point; if I want to 
> now start populating branch a, I have to "git checkout a" anyway, and 
> that action _will_ now be instantaneous.

Actually, I take that back, it's not instantaneous at all. 1995 systems are 
much better at showing long delays than those fancy modern computers are!

Rene.

```

## Linus Torvalds, 2007-04-17 16:44

Subject: Re: git branch --switch?
Message-ID: <Pine.LNX.4.64.0704170938510.5473@woody.linux-foundation.org>
URL: https://gitlist.dev/e/Pine.LNX.4.64.0704170938510.5473%40woody.linux-foundation.org
In-Reply-To: <4624F4BB.7010308@gmail.com>

```


On Tue, 17 Apr 2007, Rene Herman wrote:

> On 04/17/2007 05:41 PM, Rene Herman wrote:
> 
> > You'd be quite right in saying that there isn't much point; if I want to now
> > start populating branch a, I have to "git checkout a" anyway, and that
> > action _will_ now be instantaneous.
> 
> Actually, I take that back, it's not instantaneous at all. 1995 systems are
> much better at showing long delays than those fancy modern computers are!

You'll always have at least _one_ slow checkout. If you checkout at all.

The kernel archive is 8 million lines - 240MB or so checked out. Writing 
out that 240 MB to disk is not going to be instantaneous even on a modern 
machine, although having so much memory that you can ignore the writeout 
and let it happen in the background will certainly help.

If you don't need to build the tree, you can avoid the checkout entirely, 
of course. Most git operations don't actually need a checked-out tree at 
all, and you could even have done a bare clone and just inspected it with 
git tools if that's what is wanted.

So you could have avoided the checkout completely, and done

	git clone -l -s -n <a local linux repo> local
	git branch v20 v2.6.20
	git branch a v20
	git branch b v20
	git branch c v20

and now you'd have four branches that all point to the v20 thing, and none 
of them are actually checked out. And all of that would have been 
instantaneous, even on an old machine (apart fromt he clone itself, of 
course ;)

But the moment you decide to write 240MB to disk, you'll be slow.

			Linus

```

## Randal L. Schwartz, 2007-04-17 16:50

Subject: Re: git branch --switch?
Message-ID: <86y7kr0wst.fsf@blue.stonehenge.com>
URL: https://gitlist.dev/e/86y7kr0wst.fsf%40blue.stonehenge.com
In-Reply-To: <4624F18F.6070906@gmail.com>

```
>>>>> "Rene" == Rene Herman <rene.herman@gmail.com> writes:

Rene> Well, yes, they do, and I could also do

Rene> git branch b v2.6.20
Rene> git branch c v2.6.20

Rene> directly then (right?) but I do want that "v20" branch in the middle.

What does "in the middle" mean here?

If I set:

   b = a;
   c = a;

That's the same as if I said:

   b = a;
   c = b;

Why do you think those would be different?  There's nothing recorded
when you say "git branch X Y" except to make a new X that's a copy
of the SHA1 of the old Y.

-- 
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<merlyn@stonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!

```

## Rene Herman, 2007-04-17 16:55

Subject: Re: git branch --switch?
Message-ID: <4624FBEB.5080805@gmail.com>
URL: https://gitlist.dev/e/4624FBEB.5080805%40gmail.com
In-Reply-To: <Pine.LNX.4.64.0704170938510.5473@woody.linux-foundation.org>

```
On 04/17/2007 06:44 PM, Linus Torvalds wrote:

> So you could have avoided the checkout completely, and done
> 
> 	git clone -l -s -n <a local linux repo> local
> 	git branch v20 v2.6.20
> 	git branch a v20
> 	git branch b v20
> 	git branch c v20
> 
> and now you'd have four branches that all point to the v20 thing, and none 
> of them are actually checked out. And all of that would have been 
> instantaneous, even on an old machine (apart fromt he clone itself, of 
> course ;)

Yes, this is exactly what I wanted, thank you. I'm only learning about "git 
branch" (as opposed to git checkout -b) now and it didn't in fact occur to 
me that I could specify that <start-point> although I should've noticed in 
the manpage and certainly in Lars' reply from a few minutes back -- managed 
to go into that and still not have it click.

Nothing left for me to desire...

Rene.

```

## Lars Hjemli, 2007-04-17 17:00

Subject: Re: git branch --switch?
Message-ID: <8c5c35580704171000g74d8944w7c7b3d07228cac2@mail.gmail.com>
URL: https://gitlist.dev/e/8c5c35580704171000g74d8944w7c7b3d07228cac2%40mail.gmail.com
In-Reply-To: <4624F18F.6070906@gmail.com>

```
On 4/17/07, Rene Herman <rene.herman@gmail.com> wrote:
> On 04/17/2007 05:55 PM, Lars Hjemli wrote:
> > I might have misunderstood your goal, but have you tried
> >
> > git clone -l -s -n <a local linux repo> local
> > git branch a v2.6.20
> > git branch b a
> > git branch c a
> >
> > Now a, b and c all point at v2.6.20, while HEAD points as master.
>
> Well, yes, they do, and I could also do
>
> git branch b v2.6.20
> git branch c v2.6.20
>
> directly then (right?)

Yes


> but I do want that "v20" branch in the middle. The
> cloned repo is a linus repo, and that v20 is where I'll be pulling 2.6.20.y
> updates into; a merge branch will then merge v20, a, b and c into what I
> will be compiling.

Ok. Then maybe you want to try something like this:

$ git clone -l -s -n ../linux-2.6 rene
$ cd rene
$ git remote add v20
git://git.kernel.org/pub/scm/linux/kernel/git/stable/linux-2.6.20.y
$ git fetch v20

This gives you a tracking branch for 2.6.20.y, named as "v20/master".
That branch can then be used as a starting point for your a, b and c
branches, like:

$ git checkout -b a v20/master    # this _will_ take some time...

After applying some changes on branch a, you can then merge the latest
changes on the v20-branch like this

$ git fetch v20
$ git merge v20/master

If you want the merge to occur on a separate branch, do this first:

$ git checkout -b tmp a


--
larsh

```

## Rene Herman, 2007-04-17 17:03

Subject: Re: git branch --switch?
Message-ID: <4624FDF2.6020003@gmail.com>
URL: https://gitlist.dev/e/4624FDF2.6020003%40gmail.com
In-Reply-To: <86y7kr0wst.fsf@blue.stonehenge.com>

```
On 04/17/2007 06:50 PM, Randal L. Schwartz wrote:

> Rene> directly then (right?) but I do want that "v20" branch in the middle.
> 
> What does "in the middle" mean here?

In the middle of my mental model if nothing else :-) But never mind, I was 
confused; ignored Lars' info about branch starting points (and even myself 
stating it before) but given them, I have all I want.

Rene.

```

## Rene Herman, 2007-04-17 17:27

Subject: Re: git branch --switch?
Message-ID: <4625039F.7000804@gmail.com>
URL: https://gitlist.dev/e/4625039F.7000804%40gmail.com
In-Reply-To: <8c5c35580704171000g74d8944w7c7b3d07228cac2@mail.gmail.com>

```
On 04/17/2007 07:00 PM, Lars Hjemli wrote:

> Ok. Then maybe you want to try something like this:
> 
> $ git clone -l -s -n ../linux-2.6 rene
> $ cd rene
> $ git remote add v20
> git://git.kernel.org/pub/scm/linux/kernel/git/stable/linux-2.6.20.y
> $ git fetch v20
> 
> This gives you a tracking branch for 2.6.20.y, named as "v20/master".
> That branch can then be used as a starting point for your a, b and c
> branches, like:
> 
> $ git checkout -b a v20/master    # this _will_ take some time...
> 
> After applying some changes on branch a, you can then merge the latest
> changes on the v20-branch like this
> 
> $ git fetch v20
> $ git merge v20/master
> 
> If you want the merge to occur on a separate branch, do this first:
> 
> $ git checkout -b tmp a

Thanks much! This is very useful.

Rene.

```
