同一目录中的两个不同的 Git 存储库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8778796/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 06:19:28  来源:igfitidea点击:

Two different Git repo in same directory

git

提问by Shafiul

I want to maintain two different git repos. The repos should stay in the same root directory. How to achieve it?

我想维护两个不同的 git 存储库。存储库应保留在同一根目录中。如何实现?

What I'm willing is: to manage two repos that differ slightly. Can I have two completely different repos in same folder?

我愿意的是:管理两个略有不同的存储库。我可以在同一个文件夹中有两个完全不同的存储库吗?

Thanks for caring :)

谢谢关心:)

采纳答案by Adam Dymitruk

You can achieve this by adding using one of these two options on the git command itself:

您可以通过在 git 命令本身上添加使用这两个选项之一来实现此目的:

git --work-tree=where/my/code/is --git-dir=some/path/to/my/.git status

This would allow you to have 2 separate repos share the same working folder.

这将允许您让 2 个单独的存储库共享同一个工作文件夹。

However you should be able to get what you need by using one repo with multiple branches and perhaps only push certain branches to multiple remotes.

但是,您应该能够通过使用具有多个分支的一个 repo 来获得所需的内容,并且可能只将某些分支推送到多个遥控器。

回答by Andrew

I solved this problem, which git doesn't seem to provide a good solution to, by not using git to solve it. I used a directory junctionto link a new subfolder to a subfolder of the parent folder (i.e. linking a child folder to an "uncle folder" :P). Example in Command Prompt for Windows Vista and up:

我解决了这个问题,git 似乎没有提供一个好的解决方案,因为不使用 git 来解决它。我使用目录连接将新子文件夹链接到父文件夹的子文件夹(即将子文件夹链接到“叔叔文件夹”:P)。Windows Vista 及更高版本的命令提示符中的示例:

cd CurrentFolder/
mklink /J "LinkedFolder" "../TargetFolder"

will cause LinkedFolder to point to TargetFolder (notethe quotes). Example file structure I would then use:

将导致 LinkedFolder 指向 TargetFolder (注意引号)。然后我将使用的示例文件结构:

  • root/
    • TargetFolder/
      • shared files
    • CurrentFolder/
      • LinkedFolder/
  • 根/
    • 目标文件夹/
      • 共享文件
    • 当前文件夹/
      • 链接文件夹/

"POSIX-compliant" operating systems seem to use lnor ln -sfor this functionality.

“POSIX 兼容”操作系统似乎使用lnln -s用于此功能。

It works excellently (note: the following is from my own Windows 8.1 testing):

它运行良好(注意:以下内容来自我自己的 Windows 8.1 测试):

  • the LinkedFolder does not exist before calling mklink
  • when you make the link, anything you do to the files in either the TargetFolder or the LinkedFolder will reflect in the other, as they are one and the same
  • if you delete the link (LinkedFolder), nothing happens to the actual target folder (TargetFolder)
  • if you delete the actual target folder (TargetFolder), the link will remain active (it does not get deleted); if you then try to access the link, you will simply get an error; if you recreate the actual target folder (TargetFolder) again, the link will continue to work as before!
  • LinkedFolder 在调用之前不存在 mklink
  • 当您建立链接时,您对 TargetFolder 或 LinkedFolder 中的文件所做的任何事情都将反映在另一个中,因为它们是相同的
  • 如果删除链接 (LinkedFolder),实际目标文件夹 (TargetFolder) 不会发生任何变化
  • 如果您删除实际的目标文件夹 (TargetFolder),该链接将保持活动状态(不会被删除);如果您随后尝试访问该链接,您只会收到一个错误;如果您再次重新创建实际的目标文件夹 (TargetFolder),该链接将继续像以前一样工作!

Hope this helps someone. I just learned about this feature and I love it.

希望这可以帮助某人。我刚刚了解了这个功能,我喜欢它。

See also:
NTFS Junction Point
NTFS Symbolic Link
Symbolic Link

另请参阅:
NTFS 连接点
NTFS 符号链接
符号链接