“git clone”到现有文件夹的最佳实践是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5377960/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 10:25:24  来源:igfitidea点击:

What's the best practice to "git clone" into an existing folder?

git

提问by ripper234

I have a working copy of the project, without any source control meta data. Now, I'd like to do the equivalent of git-clone into this folder, and keep my local changes.

我有一个项目的工作副本,没有任何源代码控制元数据。现在,我想在此文件夹中执行相当于 git-clone 的操作,并保留我的本地更改。

git-clone doesn't allow me to clone into an existing folder. What is the best practice here?

git-clone 不允许我克隆到现有文件夹中。这里的最佳做法是什么?

回答by amicitas

This can be done by cloning to a new directory, then moving the .gitdirectory into your existing directory.

这可以通过克隆到新目录,然后将该.git目录移动到现有目录中来完成。

If your existing directory is named "code".

如果您现有的目录被命名为“代码”。

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

This can also be done without doing a checkout during the clone command; more information can be found here.

这也可以在克隆命令期间不进行检出而完成;可在此处找到更多信息。

回答by Andreas Krey

Don't clone, fetch instead. In the repo:

不要克隆,取而代之。在回购中:

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

Then you can reset the tree to get the commit you want:

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

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

and you are like you cloned.

而你就像克隆人一样。

The interesting question here (and the one without answer): How to find out which commit your naked tree was based on, hence to which position to reset to.

这里有一个有趣的问题(以及一个没有答案的问题):如何找出您的裸树基于哪个提交,从而重置到哪个位置。

回答by alexwenzel

The following i did, to checkout master branch in an existing directory:

我做了以下操作,以在现有目录中检出 master 分支:

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

回答by jhwist

I'd git cloneto a new directory and copy the content of the existing directory to the new clone.

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

回答by user1055643

Using a temp directory is fine, but this will work if you want to avoid that step. From the root of your working directory:

使用临时目录很好,但是如果您想避免该步骤,这将起作用。从工作目录的根目录:

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

回答by return1.at

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

回答by okTalk

To clone a git repo into an empty existing directory do the following:

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

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

Notice the .at the end of your git clonecommand. That will download the repo into the current working directory.

注意命令.末尾的git clone。这会将 repo 下载到当前工作目录中。

回答by Andrew

Lots of answers already to do it the way that the OP asked. But it worth noting that doing it the opposite way around is far simpler:

已经有很多答案可以按照 OP 要求的方式进行。但值得注意的是,以相反的方式进行操作要简单得多:

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

You now have the desired target state - fresh clone + local-changes.

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

回答by Caleb

There are two approaches to this. Where possible I would start with a clean folder for your new git working directory and then copy your version of things in later. This might look something like*:

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

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

At this point you should have a pretty clean working copy with your previous working folder as the current working directory so any changes include file deletions will show up on the radar if you run git status.

此时,您应该有一个非常干净的工作副本,并将以前的工作文件夹作为当前工作目录,因此如果您运行git status.

On the other hand if you really must do it the other way around, you can get the same result with something like this:

另一方面,如果你真的必须反过来做,你可以用这样的东西得到相同的结果:

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

Either way, the first thing I would do is run something like git stashto get a copy of all your local changes set aside, then you can re-apply them and work through which ones you want to get committed.

无论哪种方式,我要做的第一件事就是运行类似的操作git stash,将所有本地更改的副本放在一边,然后您可以重新应用它们并处理您想要提交的更改。

* Both examples assume you start out on the shell in the parent directory of your project.

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

回答by Amirtha Rajan

This is the Best of all methods i came across

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

Clone just the repository's .git folder (excluding files as they are already in existing-dir) into an empty temporary directory

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

  1. git clone --no-checkout repo-path-to-clone existing-dir/existing-dir.tmp//might want --no-hardlinks for cloning local repo
  1. git clone --no-checkout repo-path-to-clone existing-dir/existing-dir.tmp// 可能需要 --no-hardlinks 来克隆本地仓库

Move the .git folder to the directory with the files. This makes existing-dira git repo.

将 .git 文件夹移动到包含文件的目录。这构成existing-dir了一个 git repo。

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

Delete the temporary directory

删除临时目录

  1. rmdir existing-dir/existing-dir.tmp

  2. cd existing-dir

  1. rmdir existing-dir/existing-dir.tmp

  2. cd existing-dir

Git thinks all files are deleted, this reverts the state of the repo to HEAD.

Git 认为所有文件都已删除,这会将 repo 的状态恢复为 HEAD。

WARNING: any local changes to the files will be lost.

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

  1. git reset --mixed HEAD
  1. git reset --mixed HEAD