一个目录中有两个 git 存储库?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/436125/
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-10 06:04:06  来源:igfitidea点击:

Two git repositories in one directory?

gitgit-submodulesgit-subrepo

提问by Joe Casadonte

Is it possible to have 2 git repositories in one directory? I'd think not, but thought I'd ask. Basically, I'd like to check in my home directory config files (e.g. .emacs) which should be common across all of the machines I work on, but have a second repository for local files (e.g. .emacs.local), which contains machine-specific configurations. The only way I can think of to do that is to have the local config in a subdirectory and ignore that subdirectory from the main git repository. Any other ideas?

是否可以在一个目录中拥有 2 个 git 存储库?我想不会,但我想我会问。基本上,我想检查我的主目录配置文件(例如 .emacs),它应该在我工作的所有机器中通用,但有一个本地文件的第二个存储库(例如 .emacs.local),其中包含特定于机器的配置。我能想到的唯一方法是将本地配置放在一个子目录中,并从主 git 存储库中忽略该子目录。还有其他想法吗?

采纳答案by Paul

If I understand what you're doing, you can handle it all in one repository, using separate branches for each machine, and a branch containing your common home directory config files.

如果我理解你在做什么,你可以在一个存储库中处理所有这些,为每台机器使用单独的分支,以及一个包含公共主目录配置文件的分支。

Initialize the repo and commit the common files to it, perhaps renaming the MASTER branch as Common. Then create a separate branch from there for each machine that you work with, and commit machine-specific files into that branch. Any time that you change your common files, merge the common branch into each of the machine branches and push to your other machines (write a script for that if there are many).

初始化 repo 并将公共文件提交给它,可能将 MASTER 分支重命名为 Common。然后为您使用的每台机器创建一个单独的分支,并将特定于机器的文件提交到该分支中。任何时候更改公共文件时,将公共分支合并到每个机器分支并推送到其他机器(如果有很多,请为此编写脚本)。

Then on each machine, checkout that machine's branch, which will also include the common config files.

然后在每台机器上,签出该机器的分支,其中也将包含通用配置文件。

回答by Chris Moschini

This article covers this relatively well:

这篇文章比较好地介绍了这一点:

https://github.com/rrrene/gitscm-next/blob/master/app/views/blog/progit/2010-04-11-environment.markdown

https://github.com/rrrene/gitscm-next/blob/master/app/views/blog/progit/2010-04-11-environment.markdown

Basically if you're working from the command-line this is simpler than you might guess. Suppose you want 2 git repos:

基本上,如果您从命令行工作,这比您想象的要简单。假设您想要 2 个 git 存储库:

.gitone
.gittwo

You could set them up like so:

你可以像这样设置它们:

git init .
mv .git .gitone
git init .
mv .git .gittwo

You could add a file and commit it to only one like so:

您可以添加一个文件并将其仅提交给一个,如下所示:

git --git-dir=.gitone add test.txt
git --git-dir=.gitone commit -m "Test"

So the options for git come first, then the command, then the git command's options. You could easily enough alias a git command like:

所以首先是 git 的选项,然后是命令,然后是 git 命令的选项。您可以轻松地为 git 命令添加别名,例如:

#!/bin/sh
alias gitone='git --git-dir=.gitone'
alias gittwo='git --git-dir=.gittwo'

So you can commit to one or the other with a bit less typing, like gitone commit -m "blah".

因此,您可以使用较少的输入来提交一个或另一个,例如gitone commit -m "blah".

What appears to get trickier is ignores. Since .gitignore normally sits in the project root, you'd need to find a way to switch this as well without switching the entire root. Or, you could use .git/info/exclude, but all the ignores you perform then won't be committed or pushed - which could screw up other users. Others using either repo might push a .gitignore, which may cause conflicts. It's not clear to me the best way to resolve these issues.

似乎变得更棘手的是忽略。由于 .gitignore 通常位于项目根目录中,因此您需要找到一种方法来切换它,而无需切换整个根目录。或者,您可以使用 .git/info/exclude,但是您执行的所有忽略都不会被提交或推送 - 这可能会搞砸其他用户。使用任一 repo 的其他人可能会推送 .gitignore,这可能会导致冲突。我不清楚解决这些问题的最佳方法。

If you prefer GUI tools like TortoiseGit you'd also have some challenges. You could write a small script that renames .gitone or .gittwo to .git temporarily so these tools' assumptions are met.

如果你更喜欢像 TortoiseGit 这样的 GUI 工具,你也会遇到一些挑战。您可以编写一个小脚本,将 .gitone 或 .gittwo 临时重命名为 .git,以便满足这些工具的假设。

回答by Hates_

Have a look at git submodule.

看看git submodule

Submodules allow foreign repositories to be embedded within a dedicated subdirectory of the source tree, always pointed at a particular commit.

子模块允许将外部存储库嵌入源树的专用子目录中,始终指向特定的提交。

回答by tzervo

It is possible by using the variable GIT_DIRbut has many caveats if you don't know what you are doing.

使用该变量是可能的,GIT_DIR但如果您不知道自己在做什么,则有很多警告。

回答by Seth Robertson

RichiH wrote a tool called vcshwhich a tool to manage dotfiles using git's fake bare repos to put more than one working directory into $HOME. Nothing to do with csh AFAIK.

RichiH 编写了一个名为vcsh的工具,该工具使用 git 的假裸存储库管理点文件,将多个工作目录放入 $HOME。与 csh AFAIK 无关。

However, if you did have multiple directories, an alternative to git-submodules (which are a pain in the best of circumstances and this example usage is not the best of circumstances) is gitslavewhich leaves the slave repos checked out on the tip of a branch at all times and doesn't required the three step process to make a change in the subsidiary repo (checkout onto the correct branch, make & commit the change, then go into the superproject and commit the new submodule commit).

但是,如果您确实有多个目录,则 git-submodules 的替代方案(在最好的情况下很痛苦,而这个示例用法并不是最好的情况)是gitslave,它使从属存储库在始终分支并且不需要三步过程来在附属仓库中进行更改(签出到正确的分支,进行并提交更改,然后进入超级项目并提交新的子模块提交)。

回答by Pat Notz

Yeah, submodules are probably what you want. Another option would be to have your working copy in a subdirectory and then point symlinks from you home directory to the files of interest.

是的,子模块可能就是你想要的。另一种选择是将您的工作副本放在子目录中,然后将您的主目录中的符号链接指向感兴趣的文件。

回答by coderofsalvation

my preferred method is using a repo in a subdir, and use recursive symbolic links:

我的首选方法是在子目录中使用 repo,并使用递归符号链接:

git clone repo1
cd somerepo
git clone repo2
cd repo2
./build

where the 'repo/build'-file looks like:

其中 ' repo/build'-file 看起来像:

#!/bin/bash 
SELF_PATH="$(dirname "$(readlink -f "
User@Repo1$ mklink /J FullPath/Repo2/FolderA FullPath/Repo1/FolderA
User@Repo1$ mklink /J FullPath/Repo2/FolderB FullPath/Repo1/FolderB
User@Repo1$ printf "/FolderA/*\n/FolderB/*\n" >> .gitignore
")" )" # get current dir cd .. && git stash && git clean -f -d '' # remove previous symlinks cp -sR "$SELF_PATH"/* ../. # create recursive symlinks in root

caution: dont use 'git add .'

注意:不要使用“git add”。

回答by user

The other option is to they on separate folders and create symbolic hard links from one folder to the other.

另一种选择是将它们放在单独的文件夹中,并创建从一个文件夹到另一个文件夹的符号硬链接。

For example, if there are the repositories:

例如,如果有存储库:

  1. Repo1/FolderA
  2. Repo1/FolderB
  1. Repo1/文件夹A
  2. 存储库 1/文件夹 B

And:

和:

  1. Repo2/FolderC
  1. Repo2/文件夹C

You may symlink the folders FolderAand FolderBfrom the Repo1 into the Repo2. For windows the command to run on the Repo1 would be:

您可以将文件夹FolderAFolderB从 Repo1符号链接到 Repo2。对于 Windows,在 Repo1 上运行的命令是:

##代码##

For the files on the main repositories you would need to symlink each one of them, also adding them to repository .gitignoreto avoid noise, unless you want to it.

对于主存储库上的文件,您需要对它们中的每一个进行符号链接,并将它们添加到存储库.gitignore以避免噪音,除非您愿意。

回答by user1810087

Disclaimer: This is not advertising. I'm the developer of the provided library.

免责声明:这不是广告。我是提供的库的开发人员。

I've created a git extension to handle cases where you want to mix multiple repositories into one folder. The advantage of the lib is, to keep track of the repositories and file conflicts. you can find it on github. There are also 2 example repositories to try it out.

我创建了一个 git 扩展来处理您想要将多个存储库混合到一个文件夹中的情况。lib 的优点是跟踪存储库和文件冲突。你可以在github上找到它。还有 2 个示例存储库可供试用。