[英]How can I set up the working directory and the local repository on different drives?
In Git, how can I set up the working directory and the local repository on different drives? 在Git中,如何在不同的驱动器上设置工作目录和本地存储库? The reason being to back up code on every local commit.
原因是在每个本地提交上备份代码。
We employ a gatekeeper model on the 'master' repository, so I can't just push any old rubbish upstream, but I would like to ensure that any old rubbish is backed up every time I make a local commit. 我们在'master'存储库上使用了一个网守模型,所以我不能只是向上游推送任何旧的垃圾,但我想确保每次进行本地提交时都备份任何旧的垃圾。
git init
's --separate-git-dir
flag git init
的--separate-git-dir
标志 The git init
command has a flag for that: git init
命令有一个标志:
--separate-git-dir=<git dir>
Instead of initializing the repository as a directory to either
$GIT_DIR
or./.git/
, create a text file there containing the path to the actual repository.而不是将存储库初始化为
$GIT_DIR
或./.git/
的目录,./.git/
创建一个包含实际存储库路径的文本文件。 This file acts as filesystem-agnostic Git symbolic link to the repository.此文件充当与文件系统无关的Git符号链接到存储库。
For this example, let's assume that /Volumes/My_USB/
is the path to a USB drive. 对于此示例,我们假设
/Volumes/My_USB/
是USB驱动器的路径。 ( /Volumes
is specific to Mac OS, but, other than this path, this example translates to other operating systems in a straightforward manner.) (
/Volumes
特定于Mac OS,但/Volumes
路径外,此示例以直接的方式转换为其他操作系统。)
To initialise a Git repository 初始化Git存储库
/Volumes/My_USB/projectA_gitdir
, /Volumes/My_USB/projectA_gitdir
, simply run 简单地跑
git init --separate-git-dir="/Volumes/My_USB/projectA_gitdir"
To fix ideas, 要想法,
inspect the contents of the .git
file : 检查
.git
文件的内容:
$ cat .git gitdir: /Volumes/My_USB/projectA_gitdir
As you can see, it's just a text file containing the path to the git directory of your repo. 如您所见,它只是一个文本文件,其中包含repo的git目录的路径。
inspect the config of your local repo, by running 通过运行检查本地仓库的配置
$ git config --local --list
You should notice a line that isn't normally present when a repo has been initialised without the --separate-git-dir
flag: 在没有
--separate-git-dir
标志的情况下初始化repo时,您应该注意到通常不存在的行:
core.worktree=<pwd>
where <pwd>
is the path to the current directory. 其中
<pwd>
是当前目录的路径。
inspect the contents of the git directory; 检查git目录的内容; if everything went well, a folder called
projectA_gitdir
must have been created on the USB drive and populated with everything that normally goes into the .git
folder: 如果一切顺利,必须在USB驱动器上创建一个名为
projectA_gitdir
的文件夹,并填充通常进入.git
文件夹的所有内容:
$ ls /Volumes/My_USB/projectA_gitdir HEAD description info refs config hooks objects
All good :) 都好 :)
Of course, you will only be able to run Git commands on this repo if the drive is accessible. 当然,如果可以访问驱动器,您将只能在此存储库上运行Git命令。 For instance, after unmounting it, here is what happens:
例如,在卸载它之后,会发生以下情况:
$ git status
fatal: Not a git repository: /Volumes/My_USB/projectA_gitdir
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.