问题
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?
回答1:
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).
Then on each machine, checkout that machine's branch, which will also include the common config files.
回答2:
This article covers this relatively well:
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:
.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:
#!/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"
.
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.
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.
回答3:
Have a look at git submodule.
Submodules allow foreign repositories to be embedded within a dedicated subdirectory of the source tree, always pointed at a particular commit.
回答4:
RichiH wrote a tool called vcsh which 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.
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 gitslave which 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).
回答5:
It is possible by using the variable GIT_DIR
but has many caveats if you don't know what you are doing.
回答6:
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.
回答7:
my preferred method is using a repo in a subdir, and use recursive symbolic links:
git clone repo1
cd somerepo
git clone repo2
cd repo2
./build
where the 'repo/build'-file looks like:
#!/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 .'
回答8:
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:
- Repo1/FolderA
- Repo1/FolderB
And:
- Repo2/FolderC
You may symlink the folders FolderA
and FolderB
from the Repo1 into the Repo2. For windows the command to run on the Repo1 would be:
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
For the files on the main repositories you would need to symlink each one of them, also adding them to repository .gitignore
to avoid noise, unless you want to it.
回答9:
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.
来源:https://stackoverflow.com/questions/436125/two-git-repositories-in-one-directory