开发者

Git repository with git directory stored elsewhere

I'm sorry if this is a duplicate, I've searched google and SO and couldn't find anything similar since it's a fairly generic set of words to search for!

What I want is to have the .git directory be outside of the working tree.

I need to do this because it'开发者_开发技巧s a 'stealth' git repository inside a project using other version control software, and unfortunately the way it is set up is with multiple projects (which I want to each be a git repository) inside one root directory, and with build scripts that like to purge files from the project directories. So far I've been versioning the root directory and ignoring all other project directories, so one of the projects was versioned, but I now want to version another project and clearly can't have multiple git repositories in the root directory (or can I? That would be a good alternative answer). Putting the .git directories elsewhere on disk would be a good solution, if it's possible.


You can specify the path to the git repository explicitly with the --git-dir global option for all git commands. When you use this option with init it usually creates a bare repository but if you supply --work-tree as well you can initialize a non-bare repository with a 'detached' working tree.

git --git-dir=/var/repo/one.git --work-tree=/var/work/one init

From then on, you still have to supply either the --git-dir option or set GIT_DIR environment variable so that git knows where the repository is as there is no git specific data at all inside the working tree, but the working tree will be determined appropriately from the git repository config.


You can link to a gitdir in an arbitrary location by creating a file called .git in the root of the work tree, containing the following:

gitdir: <path-to-gitdir>

Naturally you need to have first moved the original .git directory to its exterior location.

All well-behaved git tools will honour this, without relying on environment variables, or OS-specific mechanisms such as symlinks. You should also be able to place these links at arbitrary locations in your directory hierarchy, thereby linking to multiple repositories.

Of course, such .git files will still be visible in the work tree, so this approach may not be acceptable in your case. However, if such a file gets deleted it is trivial to restore (unlike a .git directory).


Git interoperates with several other VCSes so this may not be the best way to solve your problem.

However, it is possible: Using git-config you can tell git to use another directory, other than the one containing .git, as the root of the working tree.

If you run git init one directory above the root directory foo of the other project, then you could use:

git config core.worktree ../foo

Where ../foo is the path of the foo project relative to your .git directory.

However, with a relative path, the git tools will only work in the parent directory of foo, not anywhere else in the tree. By using an absolute path you avoid this (but have to remember to change it if you ever move your project).


I'm not really sure if this is a good advice, but you can set the GIT_DIR environment variable to point to a non-bare git directory. Beware that in this setup git is not able to find the top work directory, so you must be sure to be in the correct directory when you issuing git commands.

mkdir foobar
cd foobar
git init
export GIT_DIR=$PWD/.git
cd ..
mkdir wc
cd wc
echo foo > bar.txt
git add .
git commit


I do not know if that is possible, I'm quite a newbie on VCSes.

But you can tell the other VCS to ignore files, for CVS make a .cvsignore file:

.git
*ignore_me*

for SVN use

$ svn propset svn:ignore -F .cvsignore .

Hope this helps.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜