我的目录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

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

更新:从git文档:

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

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


在下面的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

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

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

进入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 init
git remote add origin PATH/TO/REPO
git fetch
git reset origin/master  # Required when the versioned files existed in path before "git init" of this repo.
git checkout -t origin/master

注意:-t将为您设置上游分支,如果这是您想要的,而且通常是这样。


另一个简单的方法似乎对我很管用:

git clone --bare $URL .git
git config --unset core.bare

我签出现有文件到目录的主要用例是用Git控制我的Unix dotfiles。在一个新帐户中,主目录中已经有一些文件,甚至可能是我想从Git中获取的文件。


对其中一个对我有用的答案稍加修改:

git init
git remote add origin PATH/TO/REPO
git pull origin master

直接开始在主分支上工作。


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

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

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


这招对我很管用:

cd existing_folder
git init
git remote add origin path_to_your_repo.git
git add .
git commit
git push -u origin master

这是我的工作,但你应该合并远程存储库文件到本地文件:

git init
git remote add origin url-to-git
git branch --set-upstream-to=origin/master master
git fetch
git status

警告-这可能会覆盖文件。

git init     
git remote add origin PATH/TO/REPO     
git fetch     
git checkout -t origin/master -f

修改自@cmcginty的回答-没有-f对我来说没用


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

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

瞧!


以下是我正在做的:

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

我在一个新的Apache web目录(用WHM创建的帐户)中遇到了类似的问题,我计划将其用作登台web服务器。我最初需要用那里的代码库克隆我的新项目,并通过从存储库中提取来定期部署更改。

问题是该帐户已经包含了web服务器文件,如:

.bash_history
.bash_logout
.bash_profile
.bashrc
.contactemail
.cpanel/
...

...我既不想删除也不想提交到我的存储库。我需要他们待在那里不被发现。

我做了什么:

我去到我的web文件夹(existing_folder):

cd /home/existing_folder

然后:

git init
git remote add origin PATH/TO/REPO
git pull origin master
git status

它显示(正如预期的那样)许多未暂存文件的列表-那些最初已经存在于我的cPanel web帐户。

然后,多亏了这篇文章,我把这些文件的列表添加到:

**.git/info/exclude**

该文件几乎类似于.gitignore文件,允许您忽略正在暂存的文件。在此之后,我在.git/目录中没有任何东西要提交-它就像一个个人的.gitignore,其他人无法看到。

现在检查git状态返回:

On branch master
nothing to commit, working tree clean

现在我可以通过简单地从git存储库中提取更改来部署到这个web服务器。希望这能帮助一些web开发人员轻松地创建登台服务器。


当试图克隆到c/code时,我也遇到了同样的问题

但是这个文件夹里有很多项目。

我在c/code/newproject中创建了一个新文件夹 并将我的克隆体映射到这个文件夹。

Git的桌面,然后问我的用户,然后克隆好。


下面的方法对我很有效。首先,我要确保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

这个问题有很多答案,我不认为其中任何一个都包括将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

我在寻找一个不同的问题时,我偶然发现了这个问题:如何克隆到一个存在的目录,而文件不存在?

无论如何,我最近对几个服务器上的一些非源代码控制的文件副本就这样做了。

cd <target directory>
git init
git remote add origin <repository URI>
git fetch
git branch -f master origin/master
git reset
git show HEAD:.gitignore > .gitignore

这样的:

为您初始化一个空的回收目录 为要连接的存储库添加一个远程 检索存储库文件夹内容(/.git) 强制git init中的默认分支跟踪origin/master 取消获取所创建的所有更改(我不准确地理解在获取时staging所有文件的机制) 存储库的.gitignore文件中的副本(如果要使用该文件,则需要该文件)

对于我的问题,答案是git重置——Dale Forester的答案是hard HEAD。

用于任意分支和远程名称的替代指令

git init
git remote add <remotename> <repository URI>
git checkout -b <localbranchname>
git fetch <remotename> <remotebranchname>
git branch -f <localbranchname> <remotename>/<remotebranchname>
git reset
git show HEAD:.gitignore > .gitignore

如上所述,这是:

Initializes an empty repo directory for you Adds a remote for the repository you intend to connect to Sets the local branch name. Optional, but recommended to keep branches straight if you're doing more than one branch on the same environment Retrieves the repository folder contents (/.git) Forces the local branch from git checkout -b <localbranchname> to track <remote>/<remotebranchname> Unstages all changes created by the fetch (I don't precisely understand the mechanism for staging all the files on fetch) Copies in the repository's .gitignore file (which you'll want if you're going to use it)

如果您在最后执行git状态,您将看到当前目录(工作目录)中的任何文件都发生了未分阶段的更改,无论工作目录是否与存储库结构有关。