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

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


我将git克隆到一个新目录,并将现有目录的内容复制到新的克隆。


这可以通过克隆到一个新目录,然后将.git目录移动到现有目录来实现。

如果您的现有目录名为“code”。

git clone https://myrepo.com/git.git temp
mv temp/.git code/.git
rm -rf temp

这也可以在不执行克隆命令的情况下完成;更多信息可以在这里找到。


git clone your_repo tmp && mv tmp/.git . && rm -rf tmp && git reset --mixed

有两种方法。在可能的情况下,我会为你的新git工作目录创建一个干净的文件夹,然后稍后复制你的版本。这可能看起来像*:

mv $dir $dir.orig
git clone $url $dir
rsync -av --delete --exclude '.git' $dir.orig/ $dir/
rm -rf $dir.orig

在这一点上,你应该有一个非常干净的工作副本,你以前的工作文件夹作为当前的工作目录,所以任何变化,包括文件删除,都会显示在雷达上,如果你运行git status。

另一方面,如果你真的必须用另一种方式来做,你可以得到类似这样的结果:

cd $dir
git clone --no-checkout $url tempdir
mv tempdir/.git .
rmdir tempdir
git reset --mixed HEAD

无论哪种方式,我要做的第一件事是运行git stash之类的东西来获得所有本地更改的副本,然后你可以重新应用它们,并通过哪些你想要提交。

*这两个例子都假设您从项目的父目录中的shell开始。


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

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...

你就像克隆的一样。

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


使用临时目录是可以的,但是如果您想避免这一步,这样也可以。从你的工作目录的根目录:

$ rm -fr .git
$ git init
$ git remote add origin your-git-url
$ git fetch
$ git reset --mixed origin/master

如果你正在使用至少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 [my-repo]
git fetch
git checkout origin/master -ft

要将一个git repo克隆到一个空的现有目录,请执行以下步骤:

cd myfolder
git clone https://myrepo.com/git.git . 

注意。在git克隆命令的末尾。这将把repo下载到当前工作目录中。


已经有很多答案可以按照OP要求的方式来做了。但值得注意的是,用相反的方式来做要简单得多:

git clone repo-url tmp/
cp -R working/ tmp/

现在,您拥有了所需的目标状态—刷新克隆+本地更改。


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

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

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

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

这是我遇到的所有方法中最好的

只克隆存储库的.git文件夹(不包括已经在exists -dir中的文件)到一个空的临时目录

Git clone——no-checkout repo-path-to clone exists -dir/ exists -dir.tmp //可能需要——no-hardlinks用于克隆本地repo

将.git文件夹移动到文件所在的目录。 这使得exists -dir成为一个git回购。

mv existing-dir / existing-dir.tmp /。git existing-dir /

删除临时目录

删除目录existing-dir / existing-dir.tmp cd existing-dir

Git认为所有文件都被删除了,这将回购的状态恢复到HEAD。

警告:对文件的任何本地更改都将丢失。

git重置-混合头部


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

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克隆命令的最后(在那个目录下),像这样:

cd your_dir_to_clone_in/
git clone git@github.com/somerepo/ .

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

git pull origin master

git init
git remote add origin git@github.com:<user>/<repo>.git
git remote -v
git pull origin master

作为参考,来自Gitlab命令行指令:

推送一个现有文件夹

cd existing_folder
git init
git remote add origin <url>
git add .
git commit -m "Initial commit"
git push -u origin master

或者推送一个现有的Git存储库

cd existing_repo
git remote rename origin old-origin
git remote add origin <url>
git push -u origin --all
git push -u origin --tags