threads / discuss / 27864

can I use multiple worktree in same git repo ?

Subject: can I use multiple worktree in same git repo ?

## tl;dr

9 messages between Jul 20, 2011 and Jul 22, 2011.

replies: 8people: 6as markdown or json

J. Bakshi· Jul 20, 2011, 12:54 UTC · lore
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· Jul 20, 2011, 13:25 UTC · re: J. Bakshi · lore

Re: can I use multiple worktree in same git repo ?

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· Jul 20, 2011, 16:12 UTC · re: Nguyen Thai Ngoc Duy · lore

Re: can I use multiple worktree in same git repo ?

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

Show 17 quoted lines
> 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· Jul 21, 2011, 03:20 UTC · re: J. Bakshi · lore

Re: can I use multiple worktree in same git repo ?

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.

Show 9 quoted lines
> 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· Jul 21, 2011, 06:07 UTC · re: Nguyen Thai Ngoc Duy · lore

Re: can I use multiple worktree in same git repo ?

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

Show 35 quoted lines
> 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· Jul 21, 2011, 18:17 UTC · re: Nguyen Thai Ngoc Duy · lore

Re: can I use multiple worktree in same git repo ?

Nguyen Thai Ngoc Duy <pclouds@gmail.com> writes:
Show 8 quoted lines
> 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· Jul 21, 2011, 18:22 UTC · re: Jakub Narebski · lore

Re: can I use multiple worktree in same git repo ?

On Thu, Jul 21, 2011 at 11:17:57AM -0700, Jakub Narebski wrote:
Show 20 quoted lines
> 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· Jul 21, 2011, 18:50 UTC · re: Fredrik Gustafsson · lore

Re: can I use multiple worktree in same git repo ?

On Thu, 21 Jul 2011, Fredrik Gustafsson wrote:
> On Thu, Jul 21, 2011 at 11:17:57AM -0700, Jakub Narebski wrote:
Show 9 quoted lines
> > 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· Jul 22, 2011, 09:06 UTC · re: Jakub Narebski · lore

Re: can I use multiple worktree in same git repo ?

Jakub Narebski <jnareb <at> gmail.com> writes:
Show 18 quoted lines
> 
> 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.

← back to recent threads