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

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

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.


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

Tags:

Git