如何在不同的驱动器上设置工作目录和本地存储库? [英] How can I set up the working directory and the local repository on different drives?

查看:84
本文介绍了如何在不同的驱动器上设置工作目录和本地存储库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Git中,如何在不同的驱动器上设置工作目录和本地存储库? 原因是在每次本地提交时都要备份代码.

In Git, how can I set up the working directory and the local repository on different drives? 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.

推荐答案

使用git init--separate-git-dir标志

git init 命令具有相应的标志:

Use git init's --separate-git-dir flag

The git init command has a flag for that:

--separate-git-dir=<git dir>

不是将存储库初始化为任一目录 $GIT_DIR./.git/,在其中创建一个包含路径的文本文件 到实际的存储库.该文件充当与文件系统无关的Git 到存储库的符号链接.

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. This file acts as filesystem-agnostic Git symbolic link to the repository.

示例

在此示例中,假设/Volumes/My_USB/是USB驱动器的路径. (/Volumes特定于Mac OS,但除此路径外,此示例还以简单的方式转换为其他操作系统.)

Example

For this example, let's assume that /Volumes/My_USB/ is the path to a USB drive. (/Volumes is specific to Mac OS, but, other than this path, this example translates to other operating systems in a straightforward manner.)

初始化Git存储库

  • 其工作树是当前目录,
  • 其"git目录"为/Volumes/My_USB/projectA_gitdir

简单运行

git init --separate-git-dir="/Volumes/My_USB/projectA_gitdir"

要修正想法,

  • 检查.git 文件的内容:

$ cat .git
gitdir: /Volumes/My_USB/projectA_gitdir

如您所见,它只是一个文本文件,其中包含存储库git目录的路径.

As you can see, it's just a text file containing the path to the git directory of your repo.

通过运行检查本地仓库的配置

inspect the config of your local repo, by running

$ git config --local --list

您应该注意到在没有--separate-git-dir标志的情况下初始化回购协议时通常不会出现的行:

You should notice a line that isn't normally present when a repo has been initialised without the --separate-git-dir flag:

core.worktree=<pwd> 

其中<pwd>是当前目录的路径.

where <pwd> is the path to the current directory.

检查git目录的内容;如果一切顺利,则必须在USB驱动器上创建一个名为projectA_gitdir的文件夹,并填充通常进入.git文件夹的所有内容:

inspect the contents of the git directory; 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:

$ ls /Volumes/My_USB/projectA_gitdir
HEAD        description info        refs
config      hooks       objects

一切都很好:)

当然,如果驱动器可访问,您将只能在此存储库上运行Git命令.例如,卸载后,会发生以下情况:

Of course, you will only be able to run Git commands on this repo if the drive is accessible. For instance, after unmounting it, here is what happens:

$ git status
fatal: Not a git repository: /Volumes/My_USB/projectA_gitdir

这篇关于如何在不同的驱动器上设置工作目录和本地存储库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆