# How to relocate common and workdir?

2 messages from 2025-01-02 to 2025-01-06. Participants: Jörg Sommer, Eric Sunshine.
Thread: https://gitlist.dev/t/62722

## Jörg Sommer, 2025-01-02 09:30

Subject: How to relocate common and workdir?
Message-ID: <h7zchkwkm7djm5qrxnwyh7jvzymrtuzlc4rllnllrcxlrvgtx7@jiu7eiekjit5>
URL: https://gitlist.dev/e/h7zchkwkm7djm5qrxnwyh7jvzymrtuzlc4rllnllrcxlrvgtx7%40jiu7eiekjit5

```
Hello,

the KAS project currently discusses a patch [1] to relocate the common dir
and adjust the .git file in the worktree to match the new location. Is there
something else to adjust than just the .git file?

[1] https://groups.google.com/g/kas-devel/c/OM7bm-tjyLM

The background is that the git repository gets mounted inside a Docker
container at a fixed position /repo. This works fine for pure git
repositories, but with shared worktrees this fails, because the location in
the file /repo/.git is not inside the container.

Now, the idea is to mount $(git rev-parse --common-dir) at /repo-common
inside the container, and overlay /repo/.git (via bind-mount) with a new
file that refers to /repo-common: `sed "s|gitdir: ${git_com_dir}/|gitdir:
/repo-common/|" "${KAS_REPO_DIR}/.git"`.

I saw that there are also the files *commondir* and *gitdir* in
*.git/worktrees/…*. I suspect there are circumstances these files need
adjustments, too.

Is it really that easy and safe to modify the .git file and point to a new
location of the common-dir, or has to be done more?


Thanks for your help.

Kind regards, Jörg

-- 
Mit Statistik kann man alles zeigen – sogar das Gegenteil.

```

## Eric Sunshine, 2025-01-06 04:17

Subject: Re: How to relocate common and workdir?
Message-ID: <CAPig+cSVmO5+FtVMyB_0tECkmrEKT8RMnQRZ=y8y6-QsGqer5A@mail.gmail.com>
URL: https://gitlist.dev/e/CAPig%2BcSVmO5%2BFtVMyB_0tECkmrEKT8RMnQRZ%3Dy8y6-QsGqer5A%40mail.gmail.com
In-Reply-To: <h7zchkwkm7djm5qrxnwyh7jvzymrtuzlc4rllnllrcxlrvgtx7@jiu7eiekjit5>

```
On Thu, Jan 2, 2025 at 4:46 AM Jörg Sommer <joerg@jo-so.de> wrote:
> I saw that there are also the files *commondir* and *gitdir* in
> *.git/worktrees/…*. I suspect there are circumstances these files need
> adjustments, too.
>
> Is it really that easy and safe to modify the .git file and point to a new
> location of the common-dir, or has to be done more?

As long as "${repo}/worktrees/${wtid}/gitdir" and
"${worktreepath}/.git" are agreement with one another -- that is,
pointing at one another -- everything should be fine. That's all
that's needed for healthy linkage between the repository and the
worktree. You usually don't have to touch
"${repo}/worktrees/${wtid}/commondir" -- unless you have some really
funky layout -- since that normally just points up at "${repo}".

By the way, you may be interested in the new "--relative-paths" paths
option of git-worktree and the related configuration
"worktree.useRelativePaths" and "extension.relativeWorktrees" which
will be part of Git 2.48.0 release, and which was (at least in part)
motivated by the sort of use-case you describe.

```
