如何在本地分区的NFS分区和工作树中使用git存储库? [英] How to have git repository in NFS partition and working-tree in local partition?
问题描述
我的主目录位于文件服务器上的远程挂载的NFS分区中,并且经常进行备份。我希望将我的项目的git存储库放在我的主目录下(以便备份),但我希望我的工作树位于工作站的本地磁盘分区中(以便构建速度更快)。本地磁盘分区未备份。
有关如何执行此操作的任何想法?我知道我可以克隆NFS存储库并推送到它,但这似乎是不必要的矫枉过正。
它可以像创建.git符号链接一样简单本地分区到远程NFS分区中的.git目录?
您可以使用以下命令创建Git仓库:
- 工作树是本地磁盘分区上的当前路径
- ,但
.git
dir 存在使用- git-dir =< path>
或$ GIT_DIR
环境变量指定并引用您的内部路径(备份)主目录。
git init
命令考虑了 $ GIT_DIR
环境变量:
如果设置了 $ GIT_DIR
环境变量,那么它指定了要使用的路径而不是 ./。git
作为存储库的基础。
或者,您可以在你的主目录中创建你的仓库,但是添加下面的 git config
:
core.worktree
将路径设置为工作树。
这可以被GIT_WORK_TREE
环境变量和- 工作树$ c $覆盖c>命令行选项。
和
它可以是.git
目录的绝对路径或相对路径,由- git-dir
或GIT_DIR
,或自动发现。
如果- git指定了-dir
或GIT_DIR
,但没有任何- 工作树
,<$指定c $ c> GIT_WORK_TREEcore.worktree
,当前工作目录被认为是工作树的根。
请注意,即使在配置文件中设置了
.git
一个目录的子目录,它的值不同于后一个目录(例如/path/to/.git/config
将core.worktree
设置为/ $ / c $ c> / path / to
),这很可能是配置错误。
在/ path / to
目录仍将使用/不同/路径
作为工作树的根目录,并可能导致用户很大的混淆。
< blockquote>
OP增加:
它可能就像在本地分区中创建一个.git符号链接到远程NFS分区中的.git目录一样简单吗?
至少,使用设置(例如
git-dir
或core.worktree
),它可以实现相同的效果,而不依赖于操作系统特有的功能,如符号链接(这在每个操作系统上都不可用)。
<更新2018年(8年后):汤姆罗素增加在评论中
$ b
看起来,
- separate-git-dir
标志现在用于在本地工作站(NFS客户端)上初始化存储库时在NFS服务器上指向.git /
。
后续行为是奇怪的是:在NFS客户端上提交的更改不会自动传播到服务器,需要服务器上的git checkout - < file>
。
我后来发现在客户端repo中提交(或副本服务器)后,服务器repo中的
git reset --hard
相反)是使工作目录保持最新的最简单方式。
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?
解决方案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 theGIT_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
orGIT_DIR
, or automatically discovered.
If--git-dir
orGIT_DIR
are specified but none of--work-tree
,GIT_WORK_TREE
andcore.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
" hascore.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
, orcore.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 agit 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.
这篇关于如何在本地分区的NFS分区和工作树中使用git存储库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!