一个目录可以有2个git仓库吗?我觉得不行,但还是想问一下。基本上,我想要检入我的主目录配置文件(例如.emacs),这些文件应该在我工作的所有机器上都是通用的,但还想要另一个仓库来存储本地文件(例如.emacs.local),其中包含特定于机器的配置。我能想到的唯一方法是将本地配置放在子目录中,并从主git仓库中忽略该子目录。还有其他想法吗?
一个目录可以有2个git仓库吗?我觉得不行,但还是想问一下。基本上,我想要检入我的主目录配置文件(例如.emacs),这些文件应该在我工作的所有机器上都是通用的,但还想要另一个仓库来存储本地文件(例如.emacs.local),其中包含特定于机器的配置。我能想到的唯一方法是将本地配置放在子目录中,并从主git仓库中忽略该子目录。还有其他想法吗?
这篇文章讲得相对详细:
如果你是从命令行工作,那么这比你想象的要简单。假设你想要两个 git 仓库:
.gitone
.gittwo
您可以这样设置: 您可以这样设置:
git init .
mv .git .gitone
git init .
mv .git .gittwo
您可以这样将文件添加并提交到一个分支: 您可以这样将文件添加并提交到一个分支:
git --git-dir=.gitone add test.txt
git --git-dir=.gitone commit -m "Test"
所以git的选项首先出现,然后是命令,最后是git命令的选项。你可以很容易地给git命令起一个别名,例如:
因此,选项之前先写git,命令在中间,然后是git命令的选项。你可以简单地为git命令设置别名,比如:
```bash git config --global alias. ```请注意替换<alias-name>和<git-command>。
请注意,这里使用了反斜杠字符`\`来转义特殊字符,以确保在Shell中正确解释命令。
请注意,--global选项会将别名添加到全局.gitconfig文件中。
注意: 以上内容仅供参考,请根据您的具体需求进行修改。
#!/bin/sh
alias gitone='git --git-dir=.gitone'
alias gittwo='git --git-dir=.gittwo'
因此,您可以通过更少的打字来选择其中之一进行提交,例如 gitone commit -m "blah"
。
似乎变得更加棘手的是忽略文件。由于 .gitignore 通常位于项目根目录中,您需要找到一种方法来切换它,而不会切换整个根目录。或者,您可以使用 .git/info/exclude,但是您执行的所有忽略操作都不会被提交或推送 - 这可能会损坏其他用户。其他使用任何一个仓库的人可能会推送 .gitignore,这可能会导致冲突。对我来说,最好的解决方法还不清楚。
如果您喜欢像 TortoiseGit 这样的 GUI 工具,您也会遇到一些挑战。您可以编写一个小脚本,将 .gitone 或 .gittwo 临时重命名为 .git,以满足这些工具的假设。
alias gitone='git --git-dir=.gitone'
这样的。 - Chris Moschinigitone config core.excludesfile gitone.exclude
和 gitone add gitone.exclude
。我制作了一个扩展此解决方案的脚本:https://github.com/capr/multigit。 - user519179git config --global alias.youralias '!git --git-dir="/d/MyProject/_git"'
然后 git youralias status
:) - starikovs.gitignore
文件通常是设置并忘记的,那么你可以为每个repo制作不同的副本,然后将相关版本作为别名的一部分复制到目录中。这也适用于根目录中可能会发生冲突的其他文件,例如README.md
和.gitattributes
。 - thdoan请查看git子模块。
子模块允许将外部存储库嵌入源代码树的专用子目录中,始终指向特定提交。
如果你不知道自己在做什么,使用变量 GIT_DIR
是可能的,但有很多注意事项。
我偏爱在子目录中使用存储库,并使用递归符号链接的方法:
git clone repo1
cd somerepo
git clone repo2
cd repo2
./build
'repo/build'文件的格式如下:
#!/bin/bash
SELF_PATH="$(dirname "$(readlink -f "$0")" )" # 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 .'
Public SSH keys (id_ecdsa.pub
/ id_rsa.pub
/ id_ed25519.pub
, etc.) are present in your GitHub and GitLab profiles
Private SSH keys (id_ecdsa
/ id_rsa
/ id_ed25519
, etc.) are added and persisted in your OS's keychain
SSH config file has keys specified for GitHub and GitLab:
Host github.com
Hostname github.com
AddKeysToAgent yes
UseKeychain yes
IdentityFile ~/.ssh/id_ecdsa
Host gitlab.com
Hostname gitlab.com
AddKeysToAgent yes
UseKeychain yes
IdentityFile ~/.ssh/id_rsa
Initialize git in a directory:
git init
Connect git to one remote repository (located in GitHub)
git remote add origin git@github.com:your-username/your-repo.git
Rename .git
to something like .github
mv .git .github
Initialize git again
git init
Connect git to the other remote repository (located in GitLab)
git remote add origin git@gitlab.com:your-username/your-repo.git
Rename .git
to something like .gitlab
mv .git .gitlab
Verify that current directory is connected to two different remote repositories
git --git-dir=.github remote -v
git --git-dir=.gitlab remote -v
Pull remote (GitHub and GitLab) repositories
git --git-dir=.github pull origin main
git --git-dir=.gitlab pull origin main
Add a file to both repositories
git --git-dir=.github add README.md
git --git-dir=.gitlab add README.md
Write commit message
git --git-dir=.github commit -m "operational overview"
git --git-dir=.gitlab commit -m "operational overview"
Push to remote
git --git-dir=.github push -u origin main
git --git-dir=.gitlab push -u origin main
--git-dir
标志。.zprofile
、bashrc
等)中添加一个别名:export github="git --git-dir=.github"
export gitlab="git --git-dir=.gitlab"
pull
、push
、add
和commit
可以像这样执行:github pull origin main
,gitlab pull origin main
等。是的,子模块可能是你想要的。另一个选择是将你的工作副本放在子目录中,然后从你的主目录中指向感兴趣的文件。
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
对于主要仓库中的文件,您需要为每个文件创建符号链接,并将它们添加到存储库.gitignore
中以避免噪音,除非您希望保留。
声明:这不是广告。我是提供的库的开发者。
我创建了一个Git扩展,用于处理您想要将多个存储库混合到一个文件夹中的情况。该库的优点在于,可以跟踪存储库和文件冲突。您可以在github上找到它。还有两个示例存储库可供尝试。
git subtree
可以完成这项工作。 - Sydalmighty