在自定义位置设置 .git 文件夹
这是我当前的目录结构:
- mygit
- 代码
- .git
- 更多代码
- 我的代码
- code 文件夹由 git 监控,git 的文件位于通常的 code/.git 中,
- 我只处理这段代码的一部分,即 < em>mycode
我想使用 git 来管理我的代码,但我不希望将其文件放入预期的 code/morecode/mycode/.git 中,但是而不是在代码文件夹之外,例如在 mygit 中。
有什么想法如何做到这一点吗?
Here's my current dir structure:
- mygit
- code
- .git
- morecode
- mycode
- The code folder is monitored by git and git's files are in the usual code/.git
- 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
另外,您可以简单地将 .git 制作为常规文件并在其中添加路径,而不是使用符号链接:
Also, instead of using a symbolic link, you can simply make .git a regular file and put a path in it:
我原来的答案如下,但我重新考虑了这一点,我认为这并不是您真正想要的 - 我在第一次阅读时没有注意到您只需要那里的存储库的子目录。无论如何,使用
--git-dir
和朋友会很快变得令人困惑 - 例如,您需要记住,无论什么情况,仍然只有一个索引(暂存区域)你正在执行 git 命令的地方。相反,我认为您真正想要做的是使
mygit
成为指向子目录的符号链接,然后在code
目录中执行所有 git 操作。您可能需要查看
git
中的文档手册页,特别是关于--git-dir
和--work-tree
选项以及类似的GIT_DIR
的部分和 GIT_WORK_TREE 环境变量。您可以使用它们将任意目录设置为您的工作树和 git 目录。这里很容易忽略的一个问题是,如果您没有将绝对路径传递给
--git-dir
(或GIT_DIR
),它将相对于工作树,而不是当前目录。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 thecode
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 similarGIT_DIR
andGIT_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
(orGIT_DIR
) it will be relative to the working tree, not your current directory.