3

I am not asking how to move a git repository. I want to move the metadata contained within .git to somewhere else on a single machine, without affecting the rest of the world.

The goal is to have

~/gitmetadata/myproject/.git
~/gitmetadata/myproject/.gitignore
~/somecode/myproject/ # No .git here anymore

The purpose of this exercise is to place .git onto persistent storage (ssd) while the working directory holding source / object code etc is on a ram disk. I don't especially want to put the .git on the ramdisk as well, since I'd rather not have to start each reboot by cloning a remote repo.

Ideally I'm looking for a configuration option that I can write into a per-machine configuration file that says "actually, the files are over there".

I can work around this by either having a local repo that I pull from, provided I learn how to configure a git repo to do transparent forwarding, or by hacking up the filesystem unionfs style which will be slow.

Can git put the .git metadata somewhere else?

edit: One of the answers included the phrase "git config core.worktree". Typing this into google found a duplicates which my search did not, 2013, 2009

Community
  • 1
  • 1
Jon Chesterfield
  • 2,251
  • 1
  • 20
  • 30

3 Answers3

6

Move your .git directory to where you wish -lets say, /tmp/back-up-dir Replace the .git directory with a .git file that contains gitdir path.

Eg:

$ cd myProject
$ mv .git /tmp/back-up-dir/.git
$ echo "gitdir: /tmp/back-up-dir/.git" > .git
$ git config core.worktree $PWD
  • This works, and looks great up to line 4. What does git config core.worktree do? – Jon Chesterfield Aug 05 '16 at 14:30
  • 1
    Nevermind, got it. The text file tells git where to find the metadata, the core.worktree tells the metadata which files it cares about. Thanks – Jon Chesterfield Aug 05 '16 at 14:36
  • If you won't execute the last command (`git config core.worktree $PWD`), git commands still can be executed without a problem in a worktree (`myProject` in this case). The only thing that won't work is: `cd /tmp/back-up-dir/.git; git status;` will output `fatal: this operation must be run in a work tree`. This can be solved by specifying the worktree explicitly: `git --work-tree=/path/to/myProject status`, or just by adding the worktree to config (which is what the answer's last command do). – Alex Lipov Apr 08 '20 at 15:50
  • How does this `echo "gitdir: /tmp/back-up-dir/.git" > .git` compare to a bind mount of .git into the proper location? – Evan Carroll Jun 13 '22 at 18:11
5
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. This file acts as filesystem-agnostic Git symbolic link to the
    repository.

    If this is reinitialization, the repository will be moved to the specified 
    path.
Holloway
  • 6,412
  • 1
  • 26
  • 33
David Neiss
  • 8,161
  • 2
  • 20
  • 21
0

Yes, you can move your .git folder to permanent storage, and then set the $GIT_DIR environment variable to point to that location.

Note, however, that you cannot put your .gitignore files there. They live in the working directory, and they contain paths relative to the directory that they're in. (These typically get checked in, and are part of the repository, anyway.) You can use $GIT_DIR/info/exclude if you want an out-of-repository mechanism to ignore files.

Edward Thomson
  • 74,857
  • 14
  • 158
  • 187
  • No worries about .gitignore as it's checked in. It looks like $GIT_DIR would work nicely if I only had one git repo, which in fairness the OP does suggest, but toggling environment variables in different shells to keep git pointing at the right place sounds hazardous – Jon Chesterfield Aug 05 '16 at 14:24