简体   繁体   English

如何在不同的驱动器上设置工作目录和本地存储库?

[英]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'存储库上使用了一个网守模型,所以我不能只是向上游推送任何旧的垃圾,但我想确保每次进行本地提交时都备份任何旧的垃圾。

Use 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符号链接到存储库。

Example

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存储库

  • whose working tree is the current directory, 其工作树是当前目录,
  • whose "git directory" is /Volumes/My_USB/projectA_gitdir , 其“git目录”是/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.

相关问题 如何设置本地Git存储库和本地备份目录? - How do I set up a local Git repository and a local backup directory? 如何设置本地目录以镜像远程git存储库? - How to set up a local directory for mirroring a remote git repository? 如何在本地工作目录,远程工作目录和git ftp目标中拥有不同版本的文件? - How can I have different versions of a file in the local working directory, remote working directory and git ftp target? 在Xcode 4中如何将项目与我设置的本地Git存储库相关联? - in Xcode 4 how to associate a project to the local Git repository I set up? 如何将本地存储库设置为与开源项目的远程“上游”目录具有不同的根目录? - How to set up local repo to have different root directory than remote “upstream” directory of an open source project? 如何设置git以便我可以看到存储库中的内容 - How to set up git so that I can see the content in repository 如何在我的原始存储库中设置遥控器,就像它是克隆的一样? - How can I set up the remotes in my original repository as if it was cloned? 如何在已经存在的本地 git 存储库上设置 Visual Studio 2019? - How do I set up Visual Studio 2019 on a local git repository which already exists? 如何设置本地服务器以进行版本控制? - How can I set up local server for version control? 如何从其工作目录中分离本地git存储库? - How to detach the local git repository from its working directory?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM