如何克隆到非空目录?

我的目录A具有与目录B匹配的文件。目录A可能还有其他需要的文件。 目录B是git仓库。

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

我希望它只是克隆.git,既然所有文件都匹配,我可以从那里去吗?

我无法克隆到空目录,因为目录A中的文件不在目录B中,因此我想保留它们。

复制.git是不可行的,因为我想使用refs进行推/拉操作,并且我不想手动设置它们。

有什么办法吗?

更新:我认为这可行,任何人都可以看到任何问题吗? ->

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

#1楼

这是我遇到相同问题时所要做的(至少我认为这是相同问题)。 我进入目录A并运行git init

由于我不希望目录A中的文件后面跟随git,因此我编辑了.gitignore并将现有文件添加到其中。 在此之后,我运行git remote add origin '<url>' && git pull origin master等,然后将B克隆到A中,而不会打h。


#2楼

这为我工作:

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将为您设置上游分支,如果您想要的话,通常是这样。


#3楼

也许我误解了您的问题,但是如果您将文件从A复制/移动到git repo B并使用git add添加所需的文件,会不会更简单?

更新:从git文档:

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

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


#4楼

在以下Shell命令中, existing-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

#5楼

另一个简单的食谱似乎对我很有效:

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

我检出包含现有文件的目录的主要用例是使用Git控制Unix点文件。 在新帐户上,主目录中已经有一些文件,甚至可能是我想从Git获取的文件。


#6楼

对对我有用的答案之一稍作修改:

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

立即开始在master分支上工作。


#7楼

我喜欢Dale的答案 ,并且还添加了

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

浅层的深度避免了很多额外的早期开发人员投入。 新分支为我们提供了良好的可视化历史记录,该服务器中放置了一些新代码。在我看来,这是完美的用法分支。 感谢所有发布在此的人的深刻见解。


#8楼

这为我工作:

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

#9楼

这对我来说是工作,但是您应该将远程存储库文件合并到本地文件:

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

#10楼

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

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

根据@cmcginty的答案进行了修改-如果没有-f,则对我不起作用


#11楼

我刚才使用过此功能,它要求破坏性最低的命令:

cd existing-dir
git clone --bare repo-to-clone .git
git config --unset core.bare
git reset

瞧!


#12楼

这是我在做什么:

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

#13楼

我计划将新的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 status返回:

On branch master
nothing to commit, working tree clean

现在,只需从git存储库中提取内容,就可以将更改部署到此Web服务器。 希望这有助于某些Web开发人员轻松创建登台服务器。


#14楼

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

我的情况是,我有一个活动的Web树,而我试图为其创建一个远程存储库,而又不移动当前Web树中的任何文件。 这是我所做的:

  1. 转到网络树并运行git init
  2. 转到存储库的预期位置并运行: git clone --bare /path/to/web/repo
  3. 在我的远程仓库中编辑配置文件,然后删除[remote "origin"]部分。
  4. 在网络树中的[remote "origin"] .git / config [remote "origin"]添加[remote "origin"]部分,指向新的远程仓库。
发布了0 篇原创文章 · 获赞 1 · 访问量 2549

猜你喜欢

转载自blog.csdn.net/asdfgh0077/article/details/103936995
今日推荐