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

时间:2022-12-20 15:32:20

Executing the command git clone git@github.com:whatever creates a directory in my current folder named whatever, and drops the contents of the Git repository into that folder:

在我的当前文件夹中创建一个名为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来更新。我该怎么做呢?

15 个解决方案

#1


2301  

Option A:

选项一:

git clone git@github.com:whatever folder-name

Option B:

选项B:

move the .git folder, too.

移动.git文件夹。

Better yet:

更好的是:

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

把你的工作复制到其他地方,并创建一个符号链接。

#2


488  

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

我想很多人都在问这个问题。如果您在目录中,想要转储git存储库的内容,请运行:

git clone git@github.com:whatever .

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

最后指定当前文件夹为checkout文件夹。

#3


167  

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

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

git clone git@github.com:whatever .

else

其他的

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

#4


66  

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:

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

$ 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. 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目录,将该存储库的所有数据拉下来,并检查最新版本的工作副本。如果您进入新创建的open_framework目录,您将看到那里的项目文件,准备好进行处理或使用。如果您想要将存储库复制到一个名为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://协议,但是您也可以看到http(s)://或user@server:/path。使用SSH传输协议的git。

#5


12  

When you move the files to where you want them, are you also moving the .git directory? 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 /public directory are only the versions in the currently check-out commit (master branch by default).

它包含了repo和支持文件,而在您/公共目录中的项目文件只是当前签出提交(默认的主分支)的版本。

#6


10  

Clone:

克隆:

git clone git@jittre.unfuddle.com:jittre/name.git

Clone the "specific branch":

克隆的“特定分支”:

git clone -b [branch-name] git@jittre.unfuddle.com:jittre/name.git

#7


10  

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

如果您想要复制到当前文件夹,您应该尝试以下方法:

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

#8


9  

Make sure you remove the .git repository if you are trying to check thing out into the current directory.

如果要检查当前目录,请确保删除.git存储库。

rm -rf .git then git clone https://github.com/symfony/symfony-sandbox.git

然后再复制https://github.com/symfony/symfony-sandbox.git。

#9


8  

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

要将git存储库克隆到特定的文件夹中,可以使用-C 参数,例如:

git -C /httpdocs clone git@github.com:whatever

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

尽管它仍然会在它上面创建一个任意的文件夹,因此要将存储库的内容复制到当前目录中,请使用以下语法:

git git@github.com:whatever .

Note that cloning into an existing directory is only allowed 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 .git folder in your web server configuration (e.g. in .htaccess file).

由于您将克隆到可用于公共的文件夹中,所以可以考虑将您的Git存储库从工作树中分离出来——在您的web服务器配置中(例如,在.htaccess文件中),使用分离- Git -dir=< Git dir>或排除. Git文件夹。

#10


6  

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.

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

git clone <repo> <directory>

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

在本地机器上,将位于 的储存库中的存储库复制到名为 <目录> 的文件夹中。

Source: Setting up a repository

源代码:设置一个存储库。

#11


6  

To clone to Present Working Directory:

复制到目前的工作目录:

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

git克隆https://github.com/link.git

To clone to Another Directory:

复制到另一个目录:

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

git克隆https://github.com/link.git。/ Folder1 / Folder2

Hope it Helps :)

希望它能帮助:)

#12


3  

Here's how I would do it, but I have made an alias to do it for me.

我是这样做的,但我已经为我做了一个别名。

$ cd ~Downloads/git; git clone https:git.foo/poo.git

There is probably a more elegant way of doing this, however I found this to be easiest for myself.

可能有一种更优雅的方法,但是我发现这对我自己来说是最简单的。

Here's the alias I created to speed things along. I made it for zsh, but it should work just fine for bash or any other shell like fish, xyzsh, fizsh, and so on.

这是我创建的别名,用来加快速度。我是为zsh做的,但是它应该适用于bash或其他shell,如fish、xyzsh、fizsh等等。

Edit ~/.zshrc, /.bashrc, etc. with your favorite editor (mine is Leafpad, so I would write $ leafpad ~/.zshrc).

编辑~ /。zshrc,/。和你最喜欢的编辑器(我的是叶子板,所以我会写$ Leafpad ~/.zshrc)。

My personal preference, however, is to make a zsh plugin to keep track of all my aliases. You can create a personal plugin for oh-my-zsh by running these commands:

然而,我个人的偏好是制作一个zsh插件来跟踪我所有的别名。通过运行这些命令,您可以为oh-my-zsh创建一个个人插件:

$ cd ~/.oh-my-zsh/
$ cd plugins/
$ mkdir your-aliases-folder-name; cd your-aliases-folder-name
     # In my case '~/.oh-my-zsh/plugins/ev-aliases/ev-aliases'
$ leafpad your-zsh-aliases.plugin.zsh
     # Again, in my case 'ev-aliases.plugin.zsh'

Afterwards, add these lines to your newly created blank alises.plugin file:

然后,将这些行添加到新创建的空白中。插件文件:

# Git aliases
alias gc="cd ~/Downloads/git; git clone "

(From here, replace your name with mine.)

(从这里,把你的名字换成我的。)

Then, in order to get the aliases to work, they (along with zsh) have to be sourced-in (or whatever it's called). To do so, inside your custom plugin document add this:

然后,为了让别名能够工作,它们(和zsh一起)必须被导入(或者任何被调用的东西)。为此,在您的自定义插件文档中添加以下内容:

## Ev's Aliases

#### Remember to re-source zsh after making any changes with these commands:

#### These commands should also work, assuming ev-aliases have already been sourced before:

allsource="source $ZSH/oh-my-zsh.sh ; source /home/ev/.oh-my-zsh/plugins/ev-aliases/ev-aliases.plugin.zsh; clear"
sourceall="source $ZSH/oh-my-zsh.sh ; source /home/ev/.oh-my-zsh/plugins/ev-aliases/ev-aliases.plugin.zsh"
#### 

####################################

# git aliases

alias gc="cd ~/Downloads/git; git clone "
# alias gc="git clone "
# alias gc="cd /your/git/folder/or/whatever; git clone "

####################################

Save your oh-my-zsh plugin, and run allsource. If that does not seem to work, simply run source $ZSH/oh-my-zsh.sh; source /home/ev/.oh-my-zsh/plugins/ev-aliases/ev-aliases.plugin.zsh. That will load the plugin source which will allow you to use allsource from now on.

保存您的oh-my-zsh插件,并运行allsource。如果这似乎不起作用,只需运行源代码ZSH/oh-my-zsh.sh;/home/ev/.oh-my-zsh/plugins/ev-aliases/ev-aliases.plugin.zsh来源。这将加载插件源代码,它将允许您从现在开始使用allsource。


I'm in the process of making a Git repository with all of my aliases. Please feel free to check them out here: Ev's dot-files. Please feel free to fork and improve upon them to suit your needs.

我正在用我所有的别名创建一个Git仓库。请随意查看这里:Ev的档案。请随意使用叉子并加以改进以满足您的需要。

#13


0  

Let's say you want in a folder like /stuff, but your pull is creating a directory under /repo/tokens/.

假设你想要一个文件夹,比如/stuff,但是你的pull正在创建一个目录下/repo/令牌/。

You can do:

你能做什么:

mkdir /stuff
ln -s /repo/tokens /stuff

That's it. You are done.

就是这样。你是做。

#14


0  

If you are using ssh for git cloning you can use the following command.

如果使用ssh进行git克隆,可以使用以下命令。

git -C path clone git@github.com:path_to_repo.git

git -C路径复制git@github.com:path_to_repo.git。

eg: git -C /home/ubuntu/ clone git@github.com:kennethreitz/requests.git would pull the git repository for requests to your /home/ubuntu/ path.

git -C /home/ubuntu/ clone git@github.com:kennethreitz/requests.git将把git存储库用于请求您的/home/ubuntu/路径。

#15


-2  

Regarding this line from the original post:

关于这条从原来的帖子:

"I know how to move the files after I've cloned the repo, but this seems to break git"

“我知道如何在我克隆了repo之后移动文件,但这似乎让git崩溃了。”

I am able to do that and I don't see any issues so far with my add, commit, push, pull operations.

我能够做到这一点,并且在我的添加、提交、推、拉操作方面,我没有看到任何问题。

This approach is stated above, but just not broken down into steps. Here's the steps that work for me:

上面说了这个方法,但只是没有分解成步骤。以下是为我工作的步骤:

  1. clone the repo into any fresh temporary folder
  2. 将repo复制到任何新的临时文件夹中。
  3. cd into that root folder you just cloned locally
  4. 将cd放入本地克隆的根文件夹中。
  5. copy the entire contents of the folder, including the /.git directory - into any existing folder you like; (say an eclipse project that you want to merge with your repo)
  6. 复制文件夹的全部内容,包括/。git目录-您喜欢的任何现有文件夹;(假设您希望与repo合并的eclipse项目)

The existing folder you just copied the files into , is now ready to interact with git.

您刚刚将文件复制到的现有文件夹,现在已经准备好与git交互。

#1


2301  

Option A:

选项一:

git clone git@github.com:whatever folder-name

Option B:

选项B:

move the .git folder, too.

移动.git文件夹。

Better yet:

更好的是:

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

把你的工作复制到其他地方,并创建一个符号链接。

#2


488  

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

我想很多人都在问这个问题。如果您在目录中,想要转储git存储库的内容,请运行:

git clone git@github.com:whatever .

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

最后指定当前文件夹为checkout文件夹。

#3


167  

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

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

git clone git@github.com:whatever .

else

其他的

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

#4


66  

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:

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

$ 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. 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目录,将该存储库的所有数据拉下来,并检查最新版本的工作副本。如果您进入新创建的open_framework目录,您将看到那里的项目文件,准备好进行处理或使用。如果您想要将存储库复制到一个名为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://协议,但是您也可以看到http(s)://或user@server:/path。使用SSH传输协议的git。

#5


12  

When you move the files to where you want them, are you also moving the .git directory? 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 /public directory are only the versions in the currently check-out commit (master branch by default).

它包含了repo和支持文件,而在您/公共目录中的项目文件只是当前签出提交(默认的主分支)的版本。

#6


10  

Clone:

克隆:

git clone git@jittre.unfuddle.com:jittre/name.git

Clone the "specific branch":

克隆的“特定分支”:

git clone -b [branch-name] git@jittre.unfuddle.com:jittre/name.git

#7


10  

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

如果您想要复制到当前文件夹,您应该尝试以下方法:

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

#8


9  

Make sure you remove the .git repository if you are trying to check thing out into the current directory.

如果要检查当前目录,请确保删除.git存储库。

rm -rf .git then git clone https://github.com/symfony/symfony-sandbox.git

然后再复制https://github.com/symfony/symfony-sandbox.git。

#9


8  

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

要将git存储库克隆到特定的文件夹中,可以使用-C 参数,例如:

git -C /httpdocs clone git@github.com:whatever

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

尽管它仍然会在它上面创建一个任意的文件夹,因此要将存储库的内容复制到当前目录中,请使用以下语法:

git git@github.com:whatever .

Note that cloning into an existing directory is only allowed 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 .git folder in your web server configuration (e.g. in .htaccess file).

由于您将克隆到可用于公共的文件夹中,所以可以考虑将您的Git存储库从工作树中分离出来——在您的web服务器配置中(例如,在.htaccess文件中),使用分离- Git -dir=< Git dir>或排除. Git文件夹。

#10


6  

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.

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

git clone <repo> <directory>

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

在本地机器上,将位于 的储存库中的存储库复制到名为 <目录> 的文件夹中。

Source: Setting up a repository

源代码:设置一个存储库。

#11


6  

To clone to Present Working Directory:

复制到目前的工作目录:

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

git克隆https://github.com/link.git

To clone to Another Directory:

复制到另一个目录:

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

git克隆https://github.com/link.git。/ Folder1 / Folder2

Hope it Helps :)

希望它能帮助:)

#12


3  

Here's how I would do it, but I have made an alias to do it for me.

我是这样做的,但我已经为我做了一个别名。

$ cd ~Downloads/git; git clone https:git.foo/poo.git

There is probably a more elegant way of doing this, however I found this to be easiest for myself.

可能有一种更优雅的方法,但是我发现这对我自己来说是最简单的。

Here's the alias I created to speed things along. I made it for zsh, but it should work just fine for bash or any other shell like fish, xyzsh, fizsh, and so on.

这是我创建的别名,用来加快速度。我是为zsh做的,但是它应该适用于bash或其他shell,如fish、xyzsh、fizsh等等。

Edit ~/.zshrc, /.bashrc, etc. with your favorite editor (mine is Leafpad, so I would write $ leafpad ~/.zshrc).

编辑~ /。zshrc,/。和你最喜欢的编辑器(我的是叶子板,所以我会写$ Leafpad ~/.zshrc)。

My personal preference, however, is to make a zsh plugin to keep track of all my aliases. You can create a personal plugin for oh-my-zsh by running these commands:

然而,我个人的偏好是制作一个zsh插件来跟踪我所有的别名。通过运行这些命令,您可以为oh-my-zsh创建一个个人插件:

$ cd ~/.oh-my-zsh/
$ cd plugins/
$ mkdir your-aliases-folder-name; cd your-aliases-folder-name
     # In my case '~/.oh-my-zsh/plugins/ev-aliases/ev-aliases'
$ leafpad your-zsh-aliases.plugin.zsh
     # Again, in my case 'ev-aliases.plugin.zsh'

Afterwards, add these lines to your newly created blank alises.plugin file:

然后,将这些行添加到新创建的空白中。插件文件:

# Git aliases
alias gc="cd ~/Downloads/git; git clone "

(From here, replace your name with mine.)

(从这里,把你的名字换成我的。)

Then, in order to get the aliases to work, they (along with zsh) have to be sourced-in (or whatever it's called). To do so, inside your custom plugin document add this:

然后,为了让别名能够工作,它们(和zsh一起)必须被导入(或者任何被调用的东西)。为此,在您的自定义插件文档中添加以下内容:

## Ev's Aliases

#### Remember to re-source zsh after making any changes with these commands:

#### These commands should also work, assuming ev-aliases have already been sourced before:

allsource="source $ZSH/oh-my-zsh.sh ; source /home/ev/.oh-my-zsh/plugins/ev-aliases/ev-aliases.plugin.zsh; clear"
sourceall="source $ZSH/oh-my-zsh.sh ; source /home/ev/.oh-my-zsh/plugins/ev-aliases/ev-aliases.plugin.zsh"
#### 

####################################

# git aliases

alias gc="cd ~/Downloads/git; git clone "
# alias gc="git clone "
# alias gc="cd /your/git/folder/or/whatever; git clone "

####################################

Save your oh-my-zsh plugin, and run allsource. If that does not seem to work, simply run source $ZSH/oh-my-zsh.sh; source /home/ev/.oh-my-zsh/plugins/ev-aliases/ev-aliases.plugin.zsh. That will load the plugin source which will allow you to use allsource from now on.

保存您的oh-my-zsh插件,并运行allsource。如果这似乎不起作用,只需运行源代码ZSH/oh-my-zsh.sh;/home/ev/.oh-my-zsh/plugins/ev-aliases/ev-aliases.plugin.zsh来源。这将加载插件源代码,它将允许您从现在开始使用allsource。


I'm in the process of making a Git repository with all of my aliases. Please feel free to check them out here: Ev's dot-files. Please feel free to fork and improve upon them to suit your needs.

我正在用我所有的别名创建一个Git仓库。请随意查看这里:Ev的档案。请随意使用叉子并加以改进以满足您的需要。

#13


0  

Let's say you want in a folder like /stuff, but your pull is creating a directory under /repo/tokens/.

假设你想要一个文件夹,比如/stuff,但是你的pull正在创建一个目录下/repo/令牌/。

You can do:

你能做什么:

mkdir /stuff
ln -s /repo/tokens /stuff

That's it. You are done.

就是这样。你是做。

#14


0  

If you are using ssh for git cloning you can use the following command.

如果使用ssh进行git克隆,可以使用以下命令。

git -C path clone git@github.com:path_to_repo.git

git -C路径复制git@github.com:path_to_repo.git。

eg: git -C /home/ubuntu/ clone git@github.com:kennethreitz/requests.git would pull the git repository for requests to your /home/ubuntu/ path.

git -C /home/ubuntu/ clone git@github.com:kennethreitz/requests.git将把git存储库用于请求您的/home/ubuntu/路径。

#15


-2  

Regarding this line from the original post:

关于这条从原来的帖子:

"I know how to move the files after I've cloned the repo, but this seems to break git"

“我知道如何在我克隆了repo之后移动文件,但这似乎让git崩溃了。”

I am able to do that and I don't see any issues so far with my add, commit, push, pull operations.

我能够做到这一点,并且在我的添加、提交、推、拉操作方面,我没有看到任何问题。

This approach is stated above, but just not broken down into steps. Here's the steps that work for me:

上面说了这个方法,但只是没有分解成步骤。以下是为我工作的步骤:

  1. clone the repo into any fresh temporary folder
  2. 将repo复制到任何新的临时文件夹中。
  3. cd into that root folder you just cloned locally
  4. 将cd放入本地克隆的根文件夹中。
  5. copy the entire contents of the folder, including the /.git directory - into any existing folder you like; (say an eclipse project that you want to merge with your repo)
  6. 复制文件夹的全部内容,包括/。git目录-您喜欢的任何现有文件夹;(假设您希望与repo合并的eclipse项目)

The existing folder you just copied the files into , is now ready to interact with git.

您刚刚将文件复制到的现有文件夹,现在已经准备好与git交互。