Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to have git repository in NFS partition and working-tree in local partition?

Tags:

git

unix

My home directory is in a remotely-mounted NFS partition on a file-server and is routinely backed-up. I would like to have my project's git repository be under my home directory (so that it's backed-up) but I would like my working-tree to be in a local disk partition of my workstation (so that building is fast). The local disk partition isn't backed-up.

Any ideas on how to do this? I know that I can clone the NFS repository and push to it, but that seems like unnecessary overkill.

Could it be as simple as creating a .git symbolic link in the local partition to the .git directory in the remote NFS partition?

like image 565
Steve Emmerson Avatar asked Feb 05 '10 22:02

Steve Emmerson


2 Answers

You could create your Git repo with:

  • the working tree being a current path on the local disk partition
  • but the .git dir being specified with --git-dir=<path> or $GIT_DIR environment variable and referring to a path within your (backed-up) home directory.

The git init command takes into account the $GIT_DIR environment variable:

If the $GIT_DIR environment variable is set then it specifies a path to use instead of ./.git for the base of the repository.


Alternatively, you can create your repo in your home dir, but add the following git config:

core.worktree

Set the path to the root of the work tree.
This can be overridden by the GIT_WORK_TREE environment variable and the --work-tree command line option.
It can be an absolute path or a relative path to the .git directory, either specified by --git-dir or GIT_DIR, or automatically discovered.
If --git-dir or GIT_DIR are specified but none of --work-tree, GIT_WORK_TREE and core.worktree is specified, the current working directory is regarded as the root of the work tree.

Note that this variable is honored even when set in a configuration file in a ".git" subdirectory of a directory, and its value differs from the latter directory (e.g. "/path/to/.git/config" has core.worktree set to "/different/path"), which is most likely a misconfiguration.
Running git commands in "/path/to" directory will still use "/different/path" as the root of the work tree and can cause great confusion to the users.


The OP adds:

Could it be as simple as creating a .git symbolic link in the local partition to the .git directory in the remote NFS partition?

At least, with settings (like git-dir, or core.worktree), that allows to achieve the same effect without relying on the OS specific features like symbolic link (which is not available on every OS)


Update 2018 (8 years later): Tom Russell adds in the comments:

It appears that the --separate-git-dir flag is now used to point to .git/ on the NFS server when initializing the repository on the local workstation (NFS client).
Subsequent behavior is odd, though: Changes committed on the NFS client don't automatically propagate to the server, requiring a git checkout -- <file> on the server.

I subsequently figured out that a git reset --hard in the server repo after a commit in the client repo (or vice-versa) is the easiest way to bring a working directory up to date.

like image 179
VonC Avatar answered Oct 15 '22 02:10

VonC


Create your repo in your home directory as usual, then on your fast local disk, use the script git-new-workdir (on my box, under /usr/share/doc/git-core/contrib/workdir). It's not a part of the git core; it's a contributed script but your distro's git package may have installed it. Usage is:

git-new-workdir <repository> <new_workdir>

This will create a new, distinct working directory that is linked to your original repository.

like image 31
Wayne Conrad Avatar answered Oct 15 '22 02:10

Wayne Conrad