我的目录A中有与目录b匹配的文件,目录A中可能有其他需要的文件。目录B是一个git repo。

我想克隆目录B到目录A,但是git-clone不允许我这样做,因为目录是非空的。

我希望它只是克隆。git,因为所有的文件匹配我可以从那里去?

我不能克隆到一个空目录,因为我在目录A中有不在目录B中的文件,我想保留它们。

复制.git不是一个选项,因为我想用引用来推/拉,我不想手动设置它们。

有什么办法可以做到吗?

更新:我认为这是有效的,有人能看到任何问题吗?-->

cd a
git clone --no-hardlinks --no-checkout ../b a.tmp 
mv a.tmp/.git .
rm -rf a.tmp
git unstage # apparently git thinks all the files are deleted if you don't do this

当前回答

我喜欢戴尔的回答,我还补充道

git clone --depth 2 --no-checkout repo-to-clone existing-dir/existing-dir.tmp
git branch dev_new214
git checkout dev_new214
git add .
git commit
git checkout dev
git merge dev_new214

浅深度避免了许多额外的早期开发提交。新的分支为我们提供了一个很好的可视化历史,其中有一些来自该服务器的新代码。在我看来,这是完美的使用分支。我感谢所有在这里发帖的人的洞察力。

其他回答

下面的方法对我很有效。首先,我要确保a目录中的文件是源代码控制的:

$ cd a
$ git init
$ git add .
$ git commit -m "..."

Then

$ git remote add origin https://URL/TO/REPO
$ git pull origin master --allow-unrelated-histories
$ git push origin master

我在几分钟前使用过这个,它需要最小的潜在破坏性命令:

cd existing-dir
git clone --bare repo-to-clone .git
git config --unset core.bare
git remote rm origin
git remote add origin repo-to-clone
git reset

瞧!

也许我误解了你的问题,但是如果你把文件从A复制/移动到git repo B,然后用git add添加所需的文件,不是更简单吗?

更新:从git文档:

仅当现有目录为空时才允许克隆到该目录。

来源:http://git-scm.com/docs/git-clone

以下是我正在做的:

git clone repo /tmp/folder
cp -rf /tmp/folder/.git /dest/folder/
cd /dest/folder
git checkout -f master

在下面的shell命令中,exists -dir是一个目录,其内容与repo-to-clone git存储库中跟踪的文件相匹配。

# Clone just the repository's .git folder (excluding files as they are already in
# `existing-dir`) into an empty temporary directory
git clone --no-checkout repo-to-clone existing-dir/existing-dir.tmp # might want --no-hardlinks for cloning local repo

# Move the .git folder to the directory with the files.
# This makes `existing-dir` a git repo.
mv existing-dir/existing-dir.tmp/.git existing-dir/

# Delete the temporary directory
rmdir existing-dir/existing-dir.tmp
cd existing-dir

# git thinks all files are deleted, this reverts the state of the repo to HEAD.
# WARNING: any local changes to the files will be lost.
git reset --hard HEAD