21

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 ssd, is it possible? how? thanks.

I guess my local working copy .git directory will grow as time goes (with the new versions) and so it make sens to have it on my bigger local hdd. The working copy doesn't grow as fast so it makes sens on the ssd as it can compile faster.

The other interesting bonus is I will then have the source code (working code and .git repo) on 2 diff drives which is more protection for a laptop....

I'm using windows

wily
  • 355
  • 1
  • 3
  • 9
  • When you say your "source code", do you actually mean your working copy? Because technically, the `.git` directory/repo also contains your source code. Also, why do you even want to do this? It is possible to have a working copy in a separate directory from the `.git` repo directory, but I'm not sure if it works across drives. Are you trying to save space on your SSD? The `.git` repo is actually compressed, and will be smaller than your working copy. You may also get decreased performance if the repo is separate on your HDD. –  Jul 28 '13 at 22:27
  • yes, basically I want to have my working copy on my fast small ssd and my .git directory/repo on my bigger (but slower) hdd – wily Jul 28 '13 at 22:32
  • If anyone wants to try to create a solution that uses Windows junction points, see [documentation here](http://support.microsoft.com/kb/205524/en-us). Also see http://technet.microsoft.com/en-US/sysinternals/bb896768. –  Jul 29 '13 at 00:35

4 Answers4

27

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
Community
  • 1
  • 1
AD7six
  • 63,116
  • 12
  • 91
  • 123
  • 1
    Out of curiosity, would it be possible to just set a symbolic link (*nix systems) or a junction point (Windows) in the working copy that points to the `.git` repo? Would that require the least amount of configuration/steps? –  Jul 28 '13 at 22:51
  • A symlink works, not sure about junction points. Since the question is about windows not sure if that is useful to the OP - still needs the `core.worktree` to be set either way though. – AD7six Jul 28 '13 at 22:54
  • 2
    Thanks AD7Six and Cupcake, I ended up trying both Junction and .git file, both work. I endend up using Junction as after a "git clone" the working copy (or the moved .git) is working fine whereas it doesn't work well with the .git file. – wily Jul 31 '13 at 02:59
  • are you using git version < 1.7.8? How git stores submodules (which is relevant to this answer as it's analagous) [changed significantly at version 1.7.8](http://stackoverflow.com/questions/17568543/git-add-doesnt-work/17747571#17747571) - good to know a junction worked for you - I'll update the answer. – AD7six Jul 31 '13 at 10:20
12

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.

4

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

Alar
  • 760
  • 4
  • 11
  • 1
    I guess most people with this issue just skip this comment when they see `init`, despite it does exactly what we need ! Thanks for the tip man. – lapin Nov 22 '17 at 07:58
1

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]
yueqiw
  • 21
  • 1