如何将 Git 存储库克隆到特定文件夹中?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/651038/
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 06:16:08  来源:igfitidea点击:

How do you clone a Git repository into a specific folder?

gitrepositorygit-clone

提问by David Smith

Executing the command git clone [email protected]:whatevercreates a directory in my current folder named whatever, and drops the contents of the Git repository into that folder:

执行该命令git clone [email protected]:whatever在我当前的文件夹中创建一个名为whatever的目录,并将 Git 存储库的内容放入该文件夹中:

/httpdocs/whatever/public

My problem is that I need the contents of the Git repository cloned into my current directory so that they appear in the proper location for the web server:

我的问题是我需要将 Git 存储库的内容克隆到我的当前目录中,以便它们出现在 Web 服务器的正确位置:

/httpdocs/public

I know how to move the files after I've cloned the repository, but this seems to break Git, and I'd like to be able to update just by calling git pull. How can I do this?

我知道在克隆存储库后如何移动文件,但这似乎破坏了 Git,我希望能够通过调用git pull. 我怎样才能做到这一点?

回答by Can Berk Güder

Option A:

选项A:

git clone [email protected]:whatever folder-name

Ergo, for right hereuse:

因此,right here使用:

git clone [email protected]:whatever .

Option B:

选项 B:

Move the .gitfolder, too. Note that the .gitfolder is hidden in most graphical file explorers, so be sure to show hidden files.

也移动.git文件夹。请注意,该.git文件夹在大多数图形文件浏览器中是隐藏的,因此请务必显示隐藏文件。

mv /where/it/is/right/now/* /where/I/want/it/
mv /where/it/is/right/now/.* /where/I/want/it/

The first line grabs all normal files, the second line grabs dot-files. It is also possibe to do it in one line by enabling dotglob (i.e. shopt -s dotglob) but that is probably a bad solution if you are asking the question this answer answers.

第一行抓取所有普通文件,第二行抓取点文件。也可以通过启用 dotglob(即shopt -s dotglob)在一行中完成,但如果您问这个答案回答的问题,这可能是一个糟糕的解决方案。

Better yet:

更好的是:

Keep your working copy somewhere else, and create a symbolic link. Like this:

将您的工作副本放在其他地方,并创建一个符号链接。像这样:

ln -s /where/it/is/right/now /the/path/I/want/to/use

For you case this would be something like:

对于你的情况,这将是这样的:

ln -sfn /opt/projectA/prod/public /httpdocs/public

Which easily could be changed to test if you wanted it, i.e.:

如果您愿意,可以轻松更改以进行测试,即:

ln -sfn /opt/projectA/test/public /httpdocs/public

without moving files around. Added -fnin case someone is copying these lines (-fis force, -navoid some often unwanted interactions with already and non-existing links).

无需移动文件。添加-fn以防有人复制这些行(-f强制, -n避免与现有和不存在的链接进行一些通常不需要的交互)。

If you just want it to work, use Option A, if someone else is going to look at what you have done, use Option C.

如果您只是想让它起作用,请使用选项 A,如果其他人要查看您所做的工作,请使用选项 C。

回答by J Wynia

The example I think a lot of people asking this question are after is this. If you are inthe directory you want the contents of the git repository dumped to, run:

我想很多人问这个问题的例子是这个。如果您位于要将 git 存储库的内容转储到的目录中,请运行:

git clone [email protected]:whatever .

The "." at the end specifies the current folder as the checkout folder.

这 ”。” 最后将当前文件夹指定为结帐文件夹。

回答by csomakk

Go into the folder.. If the folder is empty, then:

进入文件夹.. 如果文件夹为空,则:

git clone [email protected]:whatever .

else

别的

git init
git remote add origin PATH/TO/REPO
git fetch
git checkout -t origin/master

回答by Craig Gjerdingen

Basic Git Repository Cloning

基本的 Git 存储库克隆

You clone a repository with

你克隆一个存储库

git clone [url]

For example, if you want to clone the Stanford University Drupal Open Framework Git library called open_framework, you can do so like this:

例如,如果您想克隆名为 open_framework 的斯坦福大学 Drupal 开放框架 Git 库,您可以这样做:

$ git clone git://github.com/SU-SWS/open_framework.git

That creates a directory named open_framework (at your current local file system location), initializes a .git directory inside it, pulls down all the data for that repository, and checks out a working copy of the latest version. If you go into the newly created open_framework directory, you'll see the project files in there, ready to be worked on or used.

这将创建一个名为 open_framework 的目录(在您当前的本地文件系统位置),在其中初始化一个 .git 目录,拉下该存储库的所有数据,并检出最新版本的工作副本。如果您进入新创建的 open_framework 目录,您将在那里看到项目文件,准备工作或使用。

Cloning a Repository Into a Specific Local Folder

将存储库克隆到特定的本地文件夹

If you want to clone the repository into a directory named something other than open_framework, you can specify that as the next command-line option:

如果要将存储库克隆到名为 open_framework 以外的目录中,可以将其指定为下一个命令行选项:

$ git clone git:github.com/SU-SWS/open_framework.git mynewtheme

That command does the same thing as the previous one, but the target directory is called mynewtheme.

该命令与前一个命令执行相同的操作,但目标目录名为 mynewtheme。

Git has a number of different transfer protocols you can use. The previous example uses the git:// protocol, but you may also see http(s):// or user@server:/path.git, which uses the SSH transfer protocol.

Git 有许多不同的传输协议可供您使用。前面的示例使用 git:// 协议,但您也可能会看到使用 SSH 传输协议的 http(s):// 或 user@server:/path.git。

回答by kenorb

To clone git repository into a specific folder, you can use -C <path>parameter, e.g.

要将 git 存储库克隆到特定文件夹,您可以使用-C <path>参数,例如

git -C /httpdocs clone [email protected]:whatever

Although it'll still create a whateverfolder on top of it, so to clone the content of the repository into current directory, use the following syntax:

尽管它仍然会whatever在其上创建一个文件夹,因此要将存储库的内容克隆到当前目录,请使用以下语法:

cd /httpdocs
git clone [email protected]:whatever .

Note that cloning into an existing directory is onlyallowed when the directory is empty.

请注意,只有在目录为时才允许克隆到现有目录中。

Since you're cloning into folder that is accessible for public, consider separating your Git repository from your working tree by using --separate-git-dir=<git dir>or exclude .gitfolder in your web server configuration (e.g. in .htaccessfile).

由于您要克隆到可供公众访问的文件夹,请考虑通过使用--separate-git-dir=<git dir>或排除.gitWeb 服务器配置中的.htaccess文件夹(例如在文件中)将 Git 存储库与工作树分开。

回答by Sarat Chandra

To clone to Present Working Directory:

克隆到当前工作目录

git clone https://github.com/link.git

To clone to Another Directory:

克隆到另一个目录

git clone https://github.com/link.git ./Folder1/Folder2

Hope it Helps :)

希望能帮助到你 :)

回答by Selvamani

Clone:

克隆:

git clone [email protected]:jittre/name.git

Clone the "specific branch":

克隆“特定分支”:

git clone -b [branch-name] [email protected]:jittre/name.git

回答by Paul

When you move the files to where you want them, are you also moving the .gitdirectory? Depending on your OS and configuration, this directory may be hidden.

当您将文件移动到您想要的位置时,您是否也在移动.git目录?根据您的操作系统和配置,此目录可能是隐藏的。

It contains the repo and the supporting files, while the project files that are in your /publicdirectory are only the versions in the currently check-out commit (master branch by default).

它包含 repo 和支持文件,而您/public目录中的项目文件只是当前检出提交(默认为 master 分支)中的版本。

回答by Luis

If you want to clone into the current folder, you should try this:

如果你想克隆到当前文件夹,你应该试试这个:

git clone https://github.com/example/example.git ./

回答by Nagarjun

Usage

用法

git clone <repository>

Clone the repository located at the <repository> onto the local machine. The original repository can be located on the local filesystem or on a remote machine accessible via HTTP or SSH.

将位于 <repository> 的存储库克隆到本地机器上。原始存储库可以位于本地文件系统或可通过 HTTP 或 SSH 访问的远程机器上。

git clone <repo> <directory>

Clone the repository located at <repository> into the folder called <directory> on the local machine.

将位于 <repository> 的存储库克隆到本地计算机上名为 <directory> 的文件夹中。

Source: Setting up a repository

来源:设置存储库