我有一个项目的工作副本,没有任何源控制元数据。现在,我想把git-clone复制到这个文件夹中,并保留我的本地更改。

Git-clone不允许我克隆到现有的文件夹。这里的最佳实践是什么?


当前回答

通常我将首先克隆初始存储库,然后将现有文件夹中的所有内容移动到初始存储库。每次都管用。

这种方法的优点是您不会丢失初始存储库的任何内容,包括README或.gitignore。

您也可以使用下面的命令来完成步骤:

$ git clone https://github.com/your_repo.git && mv existing_folder/* your_repo

其他回答

下面我做了,在一个现有目录下签出主分支:

git init
git remote add origin [my-repo]
git fetch
git checkout origin/master -ft

如果你正在使用至少git 1.7.7(教会克隆——config选项),将当前目录转换为工作副本:

git clone example.com/my.git ./.git --mirror --config core.bare=false

这是通过:

将存储库克隆到一个新的.git文件夹中 ——mirror会像.git需要的那样,将新的克隆文件放到纯元数据文件夹中 ——配置的核心。Bare =false撤销了——mirror选项中隐含的Bare =true,从而允许存储库拥有一个相关联的工作目录,并像正常的克隆一样工作


如果您希望转换为工作副本的目录中已经存在.git元数据目录,那么这显然是行不通的。

不要克隆,而是获取。在回购中:

git init
git remote add origin $url_of_clone_source
git fetch origin
git checkout -b master --track origin/master # origin/master is clone's default

然后你可以重置树来获得你想要的提交:

git reset origin/master # or whatever commit you think is proper...

你就像克隆的一样。

这里有一个有趣的问题(也是一个没有答案的问题):如何找出你的裸树是基于哪个提交的,因此要重置到哪个位置。

你可以递归地输入以下命令行:

mkdir temp_dir   //  Create new temporary dicetory named temp_dir
git clone https://www...........git temp_dir // Clone your git repo inside it
mv temp_dir/* existing_dir // Move the recently cloned repo content from the temp_dir to your existing_dir
rm -rf temp_dir // Remove the created temporary directory

如果您正在克隆相同的存储库,那么在现有的存储库中运行以下代码片段

git pull origin master