通过 ssh 进行 git 克隆
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6167905/
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
git clone through ssh
提问by dlsa
I have a project on which I created a git repository:
我有一个项目,我在其中创建了一个 git 存储库:
$ cd myproject
$ git init
$ git add .
$ git commit
I the wanted to create a bare clone on another machine:
我想在另一台机器上创建一个裸克隆:
$ cd ..
$ git clone --bare myproject ssh://user@server:/GitRepos/myproject.git
I executed the clone but did not print any answer. I logged on to the server machine and tried to see how the files are stored. The path /GitRepos was empty, so I decided to do the clone again:
我执行了克隆但没有打印任何答案。我登录到服务器计算机并尝试查看文件的存储方式。路径 /GitRepos 是空的,所以我决定再次进行克隆:
$ git clone --bare myproject ssh://user@server:/GitRepos/myproject.git
This time the answer was :
这次的答案是:
fatal: destination path 'ssh://user@server:/GitRepos/myproject.git' already exists and is not an empty directory.
致命:目标路径 'ssh://user@server:/GitRepos/myproject.git' 已经存在并且不是空目录。
But I saw that the path was empty.
What's going on here ?
但我看到这条路是空的。
这里发生了什么 ?
回答by sdaau
This is possibly unrelated directly to the question; but one mistake I just made myself, and I see in the OP, is the URL specification ssh://user@server:/GitRepos/myproject.git
- namely, you have both a colon :
, and a forward slash /
after it signifying an absolute path.
这可能与问题没有直接关系;但我自己犯的一个错误,我在 OP 中看到的,是 URL 规范ssh://user@server:/GitRepos/myproject.git
- 即,你有一个冒号:
,/
在它表示绝对路径之后有一个正斜杠。
I then found Git clone, ssh: Could not resolve hostname – git , development – Nicolas Kuttler(as that was the error I was getting, on git
version 1.7.9.5), noting:
然后我找到了Git 克隆,ssh:无法解析主机名 – git ,开发 – Nicolas Kuttler(因为这是我在git
1.7.9.5 版本上遇到的错误),并指出:
The problem with the command I used initially was that I tried to use an scp-like syntax.
我最初使用的命令的问题是我尝试使用类似 scp 的语法。
... which was also my problem! So basically in git
with ssh
, you either use
……这也是我的问题!所以基本上在git
with ssh
,你要么使用
ssh://[email protected]/absolute/path/to/repo.git/
- just a forward slash for absolute path on server[email protected]:relative/path/to/repo.git/
- just a colon (it mustn't have thessh://
for relative path on server (relative to home dir ofusername
on server machine)
ssh://[email protected]/absolute/path/to/repo.git/
- 只是服务器上绝对路径的正斜杠[email protected]:relative/path/to/repo.git/
- 只是一个冒号(它不能有ssh://
服务器上的相对路径(相对于username
服务器机器的主目录)
Hope this helps someone,
Cheers!
希望这对某人
有所帮助,干杯!
回答by dsrdakota
For repositories on GitHub, try:
对于 GitHub 上的存储库,请尝试:
git clone ssh://[email protected]/<user>/<repository name>.git
For setting up git to clone via ssh see:
要设置 git 以通过 ssh 进行克隆,请参阅:
Generating SSH Keysand add your generated key in Account Settings -> SSH Keys
生成 SSH 密钥并在帐户设置 -> SSH 密钥中添加您生成的密钥
回答by Alec the Geek
You need to run the clone command on what you are calling the server. But I bet you are not running an ssh server on your local client so that won't work anyway. Suggest you follow this approach (check the manual 'cause I'm doing this from memory)
您需要在您调用的服务器上运行 clone 命令。但我敢打赌,您没有在本地客户端上运行 ssh 服务器,因此无论如何都行不通。建议你遵循这种方法(检查手册,因为我是凭记忆做的)
- Log into the server machine.
- Create a bare repo using
git init --bare
- On the client machine you can push your repo to the server.
git remote add origin ssh://user@server:/GitRepos/myproject.git
followed bygit push origin master
- 登录到服务器机器。
- 使用创建一个裸仓库
git init --bare
- 在客户端机器上,您可以将存储库推送到服务器。
git remote add origin ssh://user@server:/GitRepos/myproject.git
其次是git push origin master
回答by Pztar
Disclaimer: This is just a copy of a comment by bobbalubamade more visible for future visitors. It helped me more than any other answer.
免责声明:这只是bobbaluba评论的副本,让未来的访问者更容易看到。它对我的帮助比任何其他答案都多。
You have to drop the ssh://
prefix when using git clone
as an example
作为示例ssh://
使用时,您必须删除前缀git clone
git clone [email protected]:owner/repo.git
回答by Wolfer
Git 101:
git 101:
git
is a decentralized version control system. You do not necessary need a server to get up and running with git. Still you might want to do that as it looks cool, right? (It's also useful if you want to work on a single project from multiple computers.)
git
是一个去中心化的版本控制系统。您不需要服务器来启动和运行 git。你仍然可能想要这样做,因为它看起来很酷,对吧?(如果您想在多台计算机上处理单个项目,这也很有用。)
So to get a "server" running you need to run git init --bare <your_project>.git
as this will create an empty repository, which you can then import on your machines without having to muck around in config files in your .git
dir.
因此,要让“服务器”运行,您需要运行,git init --bare <your_project>.git
因为这将创建一个空的存储库,然后您可以将其导入到您的机器上,而无需在.git
目录中的配置文件中乱搞。
After this you could clone the repo on your clients as it is supposed to work, but I found that some clients (namely git-gui
) will fail to clone a repo that is completely empty. To work around this you need to run cd <your_project>.git && touch <some_random_file> && git add <some_random_file> && git commit && git push origin master
. (Note that you might need to configure your username and email for that machine's git if you hadn't done so in the past. The actual commands to run will be in the error message you get so I'll just omit them.)
在此之后,您可以克隆客户端上的 repo,因为它应该可以正常工作,但是我发现某些客户端(即git-gui
)将无法克隆完全空的 repo。要解决此问题,您需要运行cd <your_project>.git && touch <some_random_file> && git add <some_random_file> && git commit && git push origin master
. (请注意,如果您过去没有这样做,您可能需要为该机器的 git 配置您的用户名和电子邮件。要运行的实际命令将在您收到的错误消息中,因此我将省略它们。)
So at this point you can clone the repository to any machine simply by running git clone <user>@<server>:<relative_path><your_project>.git
. (As others have pointed out you might need to prefix it with ssh://
if you use the absolute path.) This assumes that you can already log in from your client to the server. (You'll also get bonus points for setting up a config file and keys for ssh
, if you intend to push a lot of stuff to the remote server.)
因此,此时您只需运行git clone <user>@<server>:<relative_path><your_project>.git
. (正如其他人指出的那样,ssh://
如果您使用绝对路径,则可能需要为其添加前缀。)这假设您已经可以从客户端登录到服务器。(ssh
如果您打算将大量内容推送到远程服务器,您还将获得为 设置配置文件和密钥的奖励积分。)
Some relevant links:
This pretty much tells you what you need to know.
And this is for those who know the basic workings of git but sometimes forget the exact syntax.
回答by Willie Cheng
try this.
尝试这个。
Step 1:
第1步:
ls -al ~/.ssh
Step 2:
第2步:
ssh-keygen
(using enter key for default value)
Step 3: To setup config file
vim /c/Users/Willie/.ssh/config
Host gitlab.com
HostName gitlab.com
User git
IdentityFile ~/.ssh/id_rsa
主机 gitlab.com 主机名
gitlab.com
用户 git
IdentityFile ~/.ssh/id_rsa
Step 4:
第四步:
git clone [email protected]:<username>/test2.git
Step 5:
When you finished Step 4
1.the test2.git file will be download done
2.you will get the new file(known_hosts) in the ~/.ssh
第 5 步:
当您完成第 4 步后
1.test2.git 文件将被下载完成
2.您将在 ~/.ssh 中获得新文件(known_hosts)
PS: I create the id_rsa and id_rsa.ub by meself and I deliver it to the Gitlab server. using both keys to any client-sides(windows and Linux).
PS: id_rsa 和 id_rsa.ub 是我自己创建的,然后提交到 Gitlab 服务器。对任何客户端(Windows 和 Linux)使用这两个密钥。
回答by Kinjal Dixit
I want to attempt an answer that includes git-flow, and three 'points' or use-cases, the git central repository, the local development and the production machine. This is not well tested.
我想尝试一个包含 git-flow 和三个“点”或用例、git 中央存储库、本地开发和生产机器的答案。 这没有经过很好的测试。
I am giving incredibly specific commands. Instead of saying <your folder>
, I will say /root/git
. The only place where I am changing the original command is replacing my specific server name with example.com
. I will explain the folders purpose is so you can adjust it accordingly. Please let me know of any confusion and I will update the answer.
我给出了令人难以置信的具体命令。与其说,不如<your folder>
说/root/git
。我更改原始命令的唯一地方是将我的特定服务器名称替换为example.com
. 我将解释文件夹的用途,以便您可以相应地进行调整。请让我知道任何混淆,我会更新答案。
The git version on the server is 1.7.1. The server is CentOS 6.3 (Final).
服务器上的git版本是1.7.1。服务器是 CentOS 6.3(最终版)。
The git version on the development machine is 1.8.1.1. This is Mac OS X 10.8.4.
开发机上的git版本是1.8.1.1。这是 Mac OS X 10.8.4。
The central repository and the production machine are on the same machine.
中央存储库和生产机器在同一台机器上。
the central repository, which svn users can related to as 'server' is configured as follows. I have a folder /root/git
where I keep all my git repositories. I want to create a git repository for a project I call 'flowers'.
中央存储库,svn 用户可以将其关联为“服务器”,其配置如下。我有一个文件夹/root/git
,用于保存所有 git 存储库。我想为我称为“花”的项目创建一个 git 存储库。
cd /root/git
git clone --bare flowers flowers.git
The git command gave two messages:
git 命令给出了两条消息:
Initialized empty Git repository in /root/git/flowers.git/
warning: You appear to have cloned an empty repository.
Nothing to worry about.
没什么可担心的。
On the development machine is configured as follows. I have a folder /home/kinjal/Sites
where I put all my projects. I now want to get the central git repository.
在开发机上配置如下。我有一个文件夹/home/kinjal/Sites
,用于放置所有项目。我现在想要获取中央 git 存储库。
cd /home/kinjal/Sites
git clone [email protected]:/root/git/flowers.git
This gets me to a point where I can start adding stuff to it. I first set up git flow
这让我可以开始向其中添加内容。我首先设置了 git flow
git flow init -d
By default this is on branch develop
. I add my code here, now. Then I need to commit to the central git repository.
默认情况下,这是在 branch 上develop
。我现在在这里添加我的代码。然后我需要提交到中央 git 存储库。
git add .
git commit -am 'initial'
git push
At this point it pushed to the develop branch. I want to also add this to the master branch.
此时它推送到开发分支。我还想将它添加到 master 分支。
git flow release start v0.0.0 develop
git flow release finish v0.0.0
git push
Note that I did nothing between the release start and release finish. And when I did the release finish I was prompted to edit two files. This pushed the develop branch to master.
请注意,我在发布开始和发布结束之间没有做任何事情。当我完成发布时,我被提示编辑两个文件。这将开发分支推到了master。
On the production site, which is on the same machine as my central git repository, I want put the repository in /var/www/vhosts/example.net
. I already have /var/www/vhosts
.
在与我的中央 git 存储库位于同一台机器上的生产站点上,我想将存储库放在/var/www/vhosts/example.net
. 我已经有了/var/www/vhosts
。
cd /var/www/vhosts
git clone file:///root/git/flowers.git example.net
If the production machine would also be on a different machine, the git clone
command would look like the one used on the development machine.
如果生产机器也位于另一台机器上,则该git clone
命令将类似于开发机器上使用的命令。
回答by Andrew T Finnell
Upfront, I am a bit lacking in my GIT skills.
首先,我的 GIT 技能有点欠缺。
That is going to clone a bare repository on your machine, which only contains the folders within .git
which is a hidden directory. execute ls -al
and you should see .git
or cd .git
inside your repository.
这将在您的机器上克隆一个裸存储库,其中仅包含.git
隐藏目录的文件夹。执行ls -al
,您应该看到.git
或cd .git
在您的存储库中。
Can you add a description of your intent so that someone with more GIT skills can help? What is it you really want to do not how you plan on doing it?
您能否添加对您的意图的描述,以便具有更多 GIT 技能的人可以提供帮助?什么是你真正想做而不是你打算怎么做?
回答by Luc Depoorter
I did : git clone --bare "/GITREPOSITORIES/RepoA" "ssh://luc@EERSTENASDS119J/volume1/RepoA" Result : fatal: destination path 'ssh://luc@EERSTENASDS119J/volume1/RepoA' already exists and is not an empty directory.
我做了: git clone --bare "/GITREPOSITORIES/RepoA" "ssh://luc@EERSTENASDS119J/volume1/RepoA" 结果:致命:目标路径 'ssh://luc@EERSTENASDS119J/volume1/RepoA' 已经存在并且是不是空目录。
The system created a directory ssh://luc@EERSTENASDS119J/volume1/RepoA in my current path.
系统在我当前的路径中创建了一个目录 ssh://luc@EERSTENASDS119J/volume1/RepoA。
So git clone did not interpret the URL specification. Used the workaround of Alec.
所以 git clone 没有解释 URL 规范。使用了 Alec 的解决方法。
回答by Alex Valverde
git clone git@server:Example/proyect.git
git clone git@server:Example/proyect.git