# can I use multiple worktree in same git repo ?

9 messages from 2011-07-20 to 2011-07-22. Participants: J. Bakshi, Nguyen Thai Ngoc Duy, Jakub Narebski, Fredrik Gustafsson, Sergio.
Thread: https://gitlist.dev/t/27864

## J. Bakshi, 2011-07-20 12:54

Subject: can I use multiple worktree in same git repo ?
Message-ID: <20110720182438.3c40cf1d@shiva.selfip.org>
URL: https://gitlist.dev/e/20110720182438.3c40cf1d%40shiva.selfip.org

```
Hello list,

Please bear with me as I am from svn domain and gradually making myself aware of git.

I have a site under htdocs which is modified by svn with the help of post-commit hook. But the svn folder structure is little different than the actual structure at filesystem.
Under htdocs/demo, say I have dir1, dir2 etc... where in svn I have the structure as 

[svn]
mysite-repo
|
|----------dir1------|trunk----src (here actually I have the contents of dir1)
|----------|---------|tags
|----------|---------|branches
|
|----------dir2------|trunk----src (here actually I have the contents of dir2)
|----------|---------|tags
|----------|---------|branches

And in file-system dir1 is mapped with (checkout from) and then (svn switch --relocate)  mysite-repo/dir1/trunk/src ;
dir2 is checkout from and then (svn switch --relocate) mysite-repo/dir2/trunk/src
and so on .....

Now the post-commit hook is tricky.

ˋˋˋˋˋˋ`
#!/bin/bash

set -e

# LANG=en_US.UTF-8 for special character support

LANG=en_US.UTF-8 /usr/bin/svn up  /var/www/demo/dir1

LANG=en_US.UTF-8 /usr/bin/svn up  /var/www/demo/dir2

LANG=en_US.UTF-8 /usr/bin/svn up  /var/www/demo/dir3
ˋˋˋˋˋˋˋˋˋˋˋˋˋˋˋ`

As a result whenever there is a commit , it updates the related folder in filesystem.
Say a commit at svn->mysite-repo->dir1->trunk->src ==> modify ==> /var/www/demo/dir1

Can I do the same in git with multiple worktree ? possible ?

Thanks

```

## Nguyen Thai Ngoc Duy, 2011-07-20 13:25

Subject: Re: can I use multiple worktree in same git repo ?
Message-ID: <CACsJy8CLRjLag65H6KQ1AUABLwiL09wNQw3VH8Y-JQnw7CqLUw@mail.gmail.com>
URL: https://gitlist.dev/e/CACsJy8CLRjLag65H6KQ1AUABLwiL09wNQw3VH8Y-JQnw7CqLUw%40mail.gmail.com
In-Reply-To: <20110720182438.3c40cf1d@shiva.selfip.org>

```
On Wed, Jul 20, 2011 at 7:54 PM, J. Bakshi <joydeep@infoservices.in> wrote:
> As a result whenever there is a commit , it updates the related folder in filesystem.
> Say a commit at svn->mysite-repo->dir1->trunk->src ==> modify ==> /var/www/demo/dir1
>
> Can I do the same in git with multiple worktree ? possible ?

Using multiple worktree with the same repo won't work in git because
the repo also have worktree-related information. But you can create a
central, bare repository, then make several clones from that. Each one
is checked out with different branches. You work on a clone and push
to the central repo. The post-commit hook in the central repo will do
pull from it, for example

for site in dir1 dir2 dir3; do
    cd /var/www/$site && /usr/bin/git pull
done

Also have a look at git-new-workdir in contrib directory of git
repository. I don't use it but you might find it useful.
-- 
Duy

```

## J. Bakshi, 2011-07-20 16:12

Subject: Re: can I use multiple worktree in same git repo ?
Message-ID: <201107201611.p6KGBvna012304@dcnode-02.unlimitedmail.net>
URL: https://gitlist.dev/e/201107201611.p6KGBvna012304%40dcnode-02.unlimitedmail.net
In-Reply-To: <CACsJy8CLRjLag65H6KQ1AUABLwiL09wNQw3VH8Y-JQnw7CqLUw@mail.gmail.com>

```
On Wed, 20 Jul 2011 20:25:55 +0700
Nguyen Thai Ngoc Duy <pclouds@gmail.com> wrote:

> On Wed, Jul 20, 2011 at 7:54 PM, J. Bakshi <joydeep@infoservices.in> wrote:
> > As a result whenever there is a commit , it updates the related folder in filesystem.
> > Say a commit at svn->mysite-repo->dir1->trunk->src ==> modify ==> /var/www/demo/dir1
> >
> > Can I do the same in git with multiple worktree ? possible ?
> 
> Using multiple worktree with the same repo won't work in git because
> the repo also have worktree-related information. But you can create a
> central, bare repository, then make several clones from that. Each one
> is checked out with different branches. You work on a clone and push
> to the central repo. The post-commit hook in the central repo will do
> pull from it, for example
> 
> for site in dir1 dir2 dir3; do
>     cd /var/www/$site && /usr/bin/git pull
> done
> 

Thanks for your response. I am afraid that I can't understand the approach clearly but I must say that I am hopeful to see a direction.
Obviously there will be a central git repo which will have its worktree under htdocs at the same server. That's why the post-receive 
hook will update the website after each push. But the issue is mapping the structure at git which is different than the structure at 
filesystem. And a push should reflect to its correct folder only.

[git]->[mysite]->dir1/trunk/src => physically map => htdocs/demo/dir1
[git]->[mysite]->dir2/trunk/src => physically map => htdocs/demo/dir2


Please tell me with little more clarification, how can I do this ?

-with kind regards

```

## Nguyen Thai Ngoc Duy, 2011-07-21 03:20

Subject: Re: can I use multiple worktree in same git repo ?
Message-ID: <CACsJy8DswCJS8YfaG=e73g1ZDVRV1H3Z0St7zaAEuuwYxgxgBQ@mail.gmail.com>
URL: https://gitlist.dev/e/CACsJy8DswCJS8YfaG%3De73g1ZDVRV1H3Z0St7zaAEuuwYxgxgBQ%40mail.gmail.com
In-Reply-To: <201107201611.p6KGBvna012304@dcnode-02.unlimitedmail.net>

```
On Wed, Jul 20, 2011 at 11:12 PM, J. Bakshi <j.bakshi@unlimitedmail.org> wrote:
> Thanks for your response. I am afraid that I can't understand the approach clearly but I must say that I am hopeful to see a direction.
> Obviously there will be a central git repo which will have its worktree under htdocs at the same server.

No, the central repository does not need worktree attached (in other
words "bare repository"). You modify in a clone from it and push
to/pull from it.

> That's why the post-receive
> hook will update the website after each push. But the issue is mapping the structure at git which is different than the structure at
> filesystem. And a push should reflect to its correct folder only.
>
> [git]->[mysite]->dir1/trunk/src => physically map => htdocs/demo/dir1
> [git]->[mysite]->dir2/trunk/src => physically map => htdocs/demo/dir2
>
>
> Please tell me with little more clarification, how can I do this ?

OK, let's create a central repository

GIT_DIR=/somewhere/safe/repo.git git init --bare

Then clone it where development happens

git clone /somewhere/safe/repo.git ~/dev
cd ~/dev

Now we create "dir1" and "dir2" branches, correponding to what you
have in the original tree

git checkout -b dir1
# put contents of dir1/trunk/src here, commit
git checkout -b dir2 master
# put contents of dir2/trunk/src here, commit

Then we push everything back to central repo

git push origin +dir1:dir1 +dir2:dir2

Then we clone it to demo sites

cd /var/www
git clone /somewhere/safe/repo.git dir1
git checkout dir1

cd /var/www
git clone /somewhere/safe/repo.git dir2
git checkout dir2

Now you can add a post commit hook to /somewhere/safe/repo.git to move
to /var/www/dir[12] and do a "pull". When you push again from ~/dev,
/var/www/dir[12] should get updated. Is that what you want?
-- 
Duy

```

## J. Bakshi, 2011-07-21 06:07

Subject: Re: can I use multiple worktree in same git repo ?
Message-ID: <20110721113702.329f73b5@shiva.selfip.org>
URL: https://gitlist.dev/e/20110721113702.329f73b5%40shiva.selfip.org
In-Reply-To: <CACsJy8DswCJS8YfaG=e73g1ZDVRV1H3Z0St7zaAEuuwYxgxgBQ@mail.gmail.com>

```
On Thu, 21 Jul 2011 10:20:13 +0700
Nguyen Thai Ngoc Duy <pclouds@gmail.com> wrote:

> On Wed, Jul 20, 2011 at 11:12 PM, J. Bakshi <j.bakshi@unlimitedmail.org> wrote:
> > Thanks for your response. I am afraid that I can't understand the approach clearly but I must say that I am hopeful to see a direction.
> > Obviously there will be a central git repo which will have its worktree under htdocs at the same server.
> 
> No, the central repository does not need worktree attached (in other
> words "bare repository"). You modify in a clone from it and push
> to/pull from it.
> 
> > That's why the post-receive
> > hook will update the website after each push. But the issue is mapping the structure at git which is different than the structure at
> > filesystem. And a push should reflect to its correct folder only.
> >
> > [git]->[mysite]->dir1/trunk/src => physically map => htdocs/demo/dir1
> > [git]->[mysite]->dir2/trunk/src => physically map => htdocs/demo/dir2
> >
> >
> > Please tell me with little more clarification, how can I do this ?
> 
> OK, let's create a central repository
> 
> GIT_DIR=/somewhere/safe/repo.git git init --bare
> 
> Then clone it where development happens
> 
> git clone /somewhere/safe/repo.git ~/dev
> cd ~/dev
> 
> Now we create "dir1" and "dir2" branches, correponding to what you
> have in the original tree
> 
> git checkout -b dir1
> # put contents of dir1/trunk/src here, commit
> git checkout -b dir2 master
> # put contents of dir2/trunk/src here, commit
> 

Just here, just here... The git should have dir1/trunk/src  which should update  /var/www/demo/dir1
and  dir2/trunk/src which should update /var/www/demo/dir2

Thanks for your detailed clarification, very nice....

```

## Jakub Narebski, 2011-07-21 18:17

Subject: Re: can I use multiple worktree in same git repo ?
Message-ID: <m3sjpzsdll.fsf@localhost.localdomain>
URL: https://gitlist.dev/e/m3sjpzsdll.fsf%40localhost.localdomain
In-Reply-To: <CACsJy8CLRjLag65H6KQ1AUABLwiL09wNQw3VH8Y-JQnw7CqLUw@mail.gmail.com>

```
Nguyen Thai Ngoc Duy <pclouds@gmail.com> writes:

> On Wed, Jul 20, 2011 at 7:54 PM, J. Bakshi <joydeep@infoservices.in> wrote:
> > As a result whenever there is a commit , it updates the related folder in filesystem.
> > Say a commit at svn->mysite-repo->dir1->trunk->src ==> modify ==> /var/www/demo/dir1
> >
> > Can I do the same in git with multiple worktree ? possible ?
> 
> Using multiple worktree with the same repo won't work in git because
> the repo also have worktree-related information. [...]
[...]
> Also have a look at git-new-workdir in contrib directory of git
> repository. I don't use it but you might find it useful.

Actually with git-new-workdir you can have multiple working
directories associated with single repository, by the way of symlinks.

Note however that if you intend to *work* in those workdirs, they
better correspond to different branches... or you can mess something
heavy.

-- 
Jakub Narebski

```

## Fredrik Gustafsson, 2011-07-21 18:22

Subject: Re: can I use multiple worktree in same git repo ?
Message-ID: <20110721182215.GA9505@kolya>
URL: https://gitlist.dev/e/20110721182215.GA9505%40kolya
In-Reply-To: <m3sjpzsdll.fsf@localhost.localdomain>

```
On Thu, Jul 21, 2011 at 11:17:57AM -0700, Jakub Narebski wrote:
> Nguyen Thai Ngoc Duy <pclouds@gmail.com> writes:
> 
> > On Wed, Jul 20, 2011 at 7:54 PM, J. Bakshi <joydeep@infoservices.in> wrote:
> > > As a result whenever there is a commit , it updates the related folder in filesystem.
> > > Say a commit at svn->mysite-repo->dir1->trunk->src ==> modify ==> /var/www/demo/dir1
> > >
> > > Can I do the same in git with multiple worktree ? possible ?
> > 
> > Using multiple worktree with the same repo won't work in git because
> > the repo also have worktree-related information. [...]
> [...]
> > Also have a look at git-new-workdir in contrib directory of git
> > repository. I don't use it but you might find it useful.
> 
> Actually with git-new-workdir you can have multiple working
> directories associated with single repository, by the way of symlinks.
> 
> Note however that if you intend to *work* in those workdirs, they
> better correspond to different branches... or you can mess something
> heavy.

It would be possible to use the .git-file feature here. Then symlinks are
avoided and the solution is portable.

Med vänliga hälsningar
Fredrik

```

## Jakub Narebski, 2011-07-21 18:50

Subject: Re: can I use multiple worktree in same git repo ?
Message-ID: <201107212050.20108.jnareb@gmail.com>
URL: https://gitlist.dev/e/201107212050.20108.jnareb%40gmail.com
In-Reply-To: <20110721182215.GA9505@kolya>

```
On Thu, 21 Jul 2011, Fredrik Gustafsson wrote:
> On Thu, Jul 21, 2011 at 11:17:57AM -0700, Jakub Narebski wrote:

> > Actually with git-new-workdir you can have multiple working
> > directories associated with single repository, by the way of symlinks.
> > 
> > Note however that if you intend to *work* in those workdirs, they
> > better correspond to different branches... or you can mess something
> > heavy.
> 
> It would be possible to use the .git-file feature here. Then symlinks are
> avoided and the solution is portable.

Actually it wouldn't.  Each new workdir has a separate HEAD and a
separate index; it is contents of .git that is symlinked, not .git
itself.

-- 
Jakub Narebski
Poland

```

## Sergio, 2011-07-22 09:06

Subject: Re: can I use multiple worktree in same git repo ?
Message-ID: <loom.20110722T105732-905@post.gmane.org>
URL: https://gitlist.dev/e/loom.20110722T105732-905%40post.gmane.org
In-Reply-To: <201107212050.20108.jnareb@gmail.com>

```
Jakub Narebski <jnareb <at> gmail.com> writes:

> 
> On Thu, 21 Jul 2011, Fredrik Gustafsson wrote:
> > On Thu, Jul 21, 2011 at 11:17:57AM -0700, Jakub Narebski wrote:
> 
> > > Actually with git-new-workdir you can have multiple working
> > > directories associated with single repository, by the way of symlinks.
> > > 
> > > Note however that if you intend to *work* in those workdirs, they
> > > better correspond to different branches... or you can mess something
> > > heavy.
> > 
> > It would be possible to use the .git-file feature here. Then symlinks are
> > avoided and the solution is portable.
> 
> Actually it wouldn't.  Each new workdir has a separate HEAD and a
> separate index; it is contents of .git that is symlinked, not .git
> itself.
> 

Is there any means to separate worktree info from the object repository? E.g., 
having a .git dir identical to that of a bare repo and a .git_workdir dir with 
the head, head logs, index and all what makes a nonbare repo different from a 
bare one? That might make it easier to transform a repo from bare to nonbare in 
addition to simplifying the logic of git-new-workdir.

```
