从另一个目录克隆Git仓库

248

我想从另一个目录克隆存储库。

假设我有一个存储库在C:/folder1C:/folder2

我想将folder1中的工作克隆到folder2中。

要执行此操作,应在命令提示符中键入什么?

似乎在克隆时通常提供URL而不是文件路径,但是,此时我只是练习并尝试使用Git。


9
你可以使用命令 git clone C:\folder1\.git folder2。你需要从想要出现folder2的目录中运行该命令。 - Akash
如果你在Windows上仍然无法工作,可能需要在存储库路径前加上 file:\\。请参见此链接:https://dev59.com/R1oU5IYBdhLWcg3we24R - Jacob H
1
这个QA真的很难找! - Qwerty
8个回答

260
cd /d c:\
git clone C:\folder1 folder2

根据git clone文档

对于本地仓库,git原生支持以下语法:

/path/to/repo.git/

file:///path/to/repo.git/

这两种语法在大多数情况下是等价的,除了前一种意味着--local选项。


6
@grahamesd,你不需要将网络文件夹映射为驱动器,因为GIT可以通过以下方式进行克隆:git clone //pc_name/git,这种方法相当好。 - vladimir_ki
你需要记住目标文件的完整路径并手动输入吗?还是有其他方法可以导航到该位置或自动完成路径?长路径很难记忆。 - Rasmus Larsen
1
@RasmusLarsen,我猜你是指源而不是目的地?相对路径应该可以工作,例如 git clone ../some/path。或者,您可以管理您的远程仓库。像这样克隆应该会自动配置一个“origin”远程仓库以供将来使用,例如 fetchpush 提交。 - Chris

39
值得一提的是,该命令在Linux上的使用方式类似:

git clone path/to/source/folder path/to/destination/folder

1
或者进入您想要克隆主项目的目录,然后执行 git clone path/to/source/folder 命令。 - Sahin

25

这些方法都对我无效。我正在 Windows 上使用 git-bash。

后来发现问题出在我的文件路径格式上。

错误的路径格式为:

git clone F:\DEV\MY_REPO\.git

正确:

git clone /F/DEV/MY_REPO/.git

这些命令需要在你想要存储库文件夹出现的文件夹中执行。


以下命令可在Bash中使用:git clone F:\DEV\MY_REPO - M.Hassan
1
对于 git-bash,您可以使用反斜杠 / 或双斜杠 \\\。但不能使用单斜杠。 - Elad Weiss

9

这不难如你所见。

14:27:05 ~$ mkdir gittests
14:27:11 ~$ cd gittests/
14:27:13 ~/gittests$ mkdir localrepo
14:27:20 ~/gittests$ cd localrepo/
14:27:21 ~/gittests/localrepo$ git init
Initialized empty Git repository in /home/andwed/gittests/localrepo/.git/
14:27:22 ~/gittests/localrepo (master #)$ cd ..
14:27:35 ~/gittests$ git clone localrepo copyoflocalrepo
Cloning into 'copyoflocalrepo'...
warning: You appear to have cloned an empty repository.
done.
14:27:42 ~/gittests$ cd copyoflocalrepo/
14:27:46 ~/gittests/copyoflocalrepo (master #)$ git status
On branch master

Initial commit

nothing to commit (create/copy files and use "git add" to track)
14:27:46 ~/gittests/copyoflocalrepo (master #)$ 

9
如果您的路径中有空格,请用双引号将其括起来:
$ git clone "//serverName/New Folder/Target" f1/

8

直接使用路径对我不起作用。

这是在MacOS上最终成功的方法:

cd ~/projects
git clone file:///Users/me/projects/myawesomerepo myawesomerepocopy

这也起作用了:
git clone file://localhost/Users/me/projects/myawesomerepo myawesomerepocopy

如果我这样做,路径本身就能够工作:

git clone --local myawesomerepo myawesomerepocopy

是的,OP的例子是Windows,但问题没有标记为Windows,所以我发帖希望能帮助Mac用户。 - kris larson

4
请使用命令git clone c:/folder1 c:/folder2来克隆文件夹。
git clone [--template=<template_directory>] [-l] [-s] [--no-hardlinks]
[-q] [-n] [--bare] [--mirror] [-o <name>] [-b <name>] [-u <upload-pack>]
[--reference <repository>] [--separate-git-dir <git dir>] [--depth <depth>]
[--[no-]single-branch] [--recursive|--recurse-submodules] [--]<repository>
[<directory>]


<repository>

    The (possibly remote) repository to clone from.
    See the URLS section below for more information on specifying repositories.
<directory>

    The name of a new directory to clone into.
    The "humanish" part of the source repository is used if no directory 
    is explicitly given (repo for /path/to/repo.git and foo for host.xz:foo/.git).
    Cloning into an existing directory is only allowed if the directory is empty.

3

我正在Windows中使用git-bash。

最简单的解决方案是将源文件夹路径中的 \ 替换为正斜杠(/):

例如:c:\dev\source 将会变成 c:/dev/source

  1. 目标文件夹上启动git-bash。

  2. 使用正斜杠的源文件夹路径运行git clone:

    git clone c:/dev/source


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接