我的目录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 init
git remote add origin url-to-git
git branch --set-upstream-to=origin/master master
git fetch
git status

其他回答

我一直在寻找类似的东西,下面是我想到的:

我的情况是,我有一个活跃的网络树,我试图为它创建一个远程存储库,而不移动当前网络树中的任何文件。以下是我所做的:

进入web树,运行git init 转到存储库的预定位置并运行:git clone——bare /path/to/web/repo 编辑远程repo中的配置文件,并删除[远程“origin”]部分。 在web树的.git/config中添加一个[remote "origin"]节,指向新的远程repo。

当我遇到同样的问题(至少我认为是同样的问题)时,我最终是这样做的。我进入目录A,运行git init。

因为我不希望目录A中的文件后面跟着git,所以我编辑了.gitignore并将现有文件添加到其中。在这之后,我运行git远程添加origin '<url>' && git拉origin master et voíla, B被“克隆”到A中,没有一个小插曲。

以下是我正在做的:

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

这个问题有很多答案,我不认为其中任何一个都包括将a初始化为Git回购,然后从B拉到a的选项。

# Turn `A` into a Git repo and check in files
git -C A init
git -C A add --all
git -C A commit -m "Add existing files"

# Set `B` as a remote
git -C A remote add local-B file:///B

# Fetch & merge B 'master' to A 'master'
git -C A pull --allow-unrelated local-B master