Git - Moving the .git directory to another drive, keep the source code where it is

前端 未结 4 1382
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-23 15:02

I have my source code and .git folder on a small fast ssd, I would like to have the .git directory on my second bigger slower hdd and keep my working code on my fast smaller

相关标签:
4条回答
  • 2020-12-23 15:18

    Yes you can

    Symlinks

    You can symlink (or use junction points) the .git dir to a different location:

    $ cd my/project
    $ mv .git /over/here/.git
    ln -s /over/here/.git .
    

    And the repository will work fine.

    gitdir:

    Or you can replace the .git folder with a file that tells git where the .git folder really is. This is exactly how git submodules are setup by default in version 1.7.8 or later.

    The steps to re-configure an existing checkout are:

    1. move the .git dir to where it needs to be
    2. replace it with a file .git containing: gitdir: path/to/.git
    3. define core.worktree to point at the working copy

    As a script that would be:

    $ cd my/project
    $ mv .git /tmp/.git
    $ echo "gitdir: /tmp/.git" > .git
    $ git config core.worktree $PWD
    

    Junctions on Windows

    Easily create junctions on Windows using junction.exe from Microsoft.

    > junction.exe c:\fast-ssd\proj\.git d:\slow-hdd\proj\.git
    
    0 讨论(0)
  • 2020-12-23 15:23

    The --separate-git-dir method is exactly the same as the gitdir approach.

    After running

    $ git init --separate-git-dir=[path to directory for .git data]
    

    what happens is that a .git file is created in the local directory with the following content:

    gitdir: [path to directory for .git data]
    
    0 讨论(0)
  • 2020-12-23 15:41

    It is possible to have a Git repository directory in a different location than the root of your working copy, but I'm not sure if it works across drives.

    You can set the repo directory to be placed in a different directory while cloning with the --separate-git-dir flag:

    $ git clone --separate-git-dir=<path to directory for repo> \
    <remote url> <path for working copy>
    

    For a repo that's already been cloned, you might be able to set a different path for the repo and/or working copy with the --git-dir=<path> and --work-tree=<path> flags for git.

    You might also want to check out the core.worktree configuration.

    0 讨论(0)
  • 2020-12-23 15:43

    Actually, you can just run

    $ git init --separate-git-dir=[path to directory for .git data]
    

    git init is safe to be used inside an already cloned repository

    Running git init in an existing repository is safe. It will not overwrite things that are already there. The primary reason for rerunning git init is to pick up newly added templates (or to move the repository to another place if --separate-git-dir is given).

    Reference to Git manual page

    0 讨论(0)
提交回复
热议问题