Setting up .git folder in a custom location
Asked Answered
B

2

3

Here's my current dir structure:

  • mygit
  • code
    • .git
    • morecode
      • mycode
  1. The code folder is monitored by git and git's files are in the usual code/.git
  2. I am working only on a piece of this code, i.e. mycode

I'd like to use git to manage my code, but I don't want its files to be put in the expected code/morecode/mycode/.git, but rather outside of the code folder, e.g. in mygit.

Any ideas how to do that?

Buote answered 17/3, 2011 at 9:15 Comment(0)
D
5

My original answer is below, but I've rethought this and I don't think that's really what you want - I hadn't noticed on first reading that you only want a subdirectory of the repository there. In any case, using --git-dir and friends gets confusing fast - you need to remember, for instance, that there's still only one index (staging area) regardless of where you're doing git commands from.

Instead, I think that really what you want to do is to make mygit a symlink that points to the subdirectory, and then do all your git operations in the code directory.


You may want to look at the documentation in the git man page, and particularly the sections on the --git-dir and --work-tree options and the similar GIT_DIR and GIT_WORK_TREE environment variables. You can use these to set arbitrary directories to be your working tree and git directories.

One gotcha that's easy to miss here is if you don't pass an absolute path to --git-dir (or GIT_DIR) it will be relative to the working tree, not your current directory.

Dombrowski answered 17/3, 2011 at 9:20 Comment(2)
Great! Seems to be exactly what I wanted! Thanks!Buote
@Albus Dumbledore: Thanks - I've actually changed my answer a bit now, but I'm still not sure I'm reading your question right. Am I right in thinking that you want mygit to correspond to morecode?Dombrowski
S
9

Also, instead of using a symbolic link, you can simply make .git a regular file and put a path in it:

$ echo gitdir: /path/to/gitdir > .git
Stores answered 17/3, 2011 at 11:7 Comment(1)
Really? Sounds interesting. Thanks!Buote
D
5

My original answer is below, but I've rethought this and I don't think that's really what you want - I hadn't noticed on first reading that you only want a subdirectory of the repository there. In any case, using --git-dir and friends gets confusing fast - you need to remember, for instance, that there's still only one index (staging area) regardless of where you're doing git commands from.

Instead, I think that really what you want to do is to make mygit a symlink that points to the subdirectory, and then do all your git operations in the code directory.


You may want to look at the documentation in the git man page, and particularly the sections on the --git-dir and --work-tree options and the similar GIT_DIR and GIT_WORK_TREE environment variables. You can use these to set arbitrary directories to be your working tree and git directories.

One gotcha that's easy to miss here is if you don't pass an absolute path to --git-dir (or GIT_DIR) it will be relative to the working tree, not your current directory.

Dombrowski answered 17/3, 2011 at 9:20 Comment(2)
Great! Seems to be exactly what I wanted! Thanks!Buote
@Albus Dumbledore: Thanks - I've actually changed my answer a bit now, but I'm still not sure I'm reading your question right. Am I right in thinking that you want mygit to correspond to morecode?Dombrowski

© 2022 - 2024 — McMap. All rights reserved.