如何克隆到非空目录? [英] How do I clone into a non-empty directory?

查看:314
本文介绍了如何克隆到非空目录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



我想将目录B克隆到目录A,但git-clone不允许我,因为该目录非空。 / p>

我希望它能克隆.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#显然git认为如果你不这样做,所有的文件都会被删除


解决方案

在以下shell命令中, existing-dir 是一个目录,文件在 repo-to-clone git仓库中。


$ b

 #将仓库的.git文件夹(不包括已存在于
#`existing-dir中的文件)复制到一个空的临时目录中
git clone --no-checkout repo-克隆existing-dir / existing-dir.tmp#可能需要-no-hardlinks克隆本地回购

#将.git文件夹移动到包含文件的目录中。
#这使得`existing-dir`成为一个git仓库。
mv现有目录/现有dir.tmp / .git现有目录/

#删除临时目录
rmdir现有目录/现有dir.tmp
cd existing-dir

#git认为所有文件都被删除,这会将回购的状态恢复为HEAD。
#警告:对文件的任何本地更改都将丢失。
git reset --hard HEAD


I have directory A with files matching directory B. Directory A may have other needed files. Directory B is a git repo.

I want to clone directory B to directory A but git-clone won't allow me to since the directory is non-empty.

I was hoping it would just clone .git and since all the files match I could go from there?

I can't clone into an empty directory because I have files in directory A that are not in directory B and I want to keep them.

Copying .git is not an option since I want refs to push/pull with and I don't want to set them up manually.

Is there any way to do this?

Update: I think this works, can anyone see any problems? -->

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

解决方案

In the following shell commands existing-dir is a directory whose contents match the tracked files in the repo-to-clone git repository.

# 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

这篇关于如何克隆到非空目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆