如何在 Linux 上设置私有 git 服务器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6249088/
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
How to set up private git server on linux
提问by xylar
I have tried following how-set-up-your-own-private-git-server-linuxand private-remote-git-repositories-ubuntu-linodebut I am still having problems.
我曾尝试遵循how-set-up-your-own-private-git-server-linux和private-remote-git-repositories-ubuntu-linode但我仍然遇到问题。
My local environment is windows and my remote linux. I have a couple of questions:
我的本地环境是 windows 和我的远程 linux。我有一些问题:
- In the first article it describes setting up a public ssh key for the server - I've never done this before and I'm not sure where to type the commands (not even sure whether local or remote!!):
ssh [email protected] mkdir .ssh
andscp ~/.ssh/id_rsa.pub [email protected]:.ssh/authorized_keys
- On my local dev machine should I be running msysgit? Is there an alternative because the version I installed is taking up 1.4 GB! I installed
msysGit-netinstall-1.7.4-preview20110204.exe
from http://code.google.com/p/msysgit/downloads/list
- 在第一篇文章中,它描述了为服务器设置公共 ssh 密钥 - 我以前从未这样做过,我不确定在哪里键入命令(甚至不确定是本地还是远程!!):
ssh [email protected] mkdir .ssh
和scp ~/.ssh/id_rsa.pub [email protected]:.ssh/authorized_keys
- 在我的本地开发机器上我应该运行msysgit吗?有没有替代方案,因为我安装的版本占用了 1.4 GB!我
msysGit-netinstall-1.7.4-preview20110204.exe
从http://code.google.com/p/msysgit/downloads/list安装
I tried skipping making the git user and public key, created the repositories on the remote machine but then when I try git remote add origin ssh://[username]@[domain/ip/hostname]/srv/git/[project-name]
as root user it says: fatal: Not a git repository (or any of the parent directories): .git
我尝试跳过制作 git 用户和公钥,在远程机器上创建了存储库,但是当我git remote add origin ssh://[username]@[domain/ip/hostname]/srv/git/[project-name]
以 root 用户身份尝试时,它说:fatal: Not a git repository (or any of the parent directories): .git
采纳答案by xylar
- I had been trying to generate the key pairs using putty but couldn't get it to work. In the end I realised that I could generate the keys via the msysgit command line using
ssh-keygen -C "[email protected]" -t rsa
. Copy the public key to the server usingnano /home/git/.ssh/authorized_keys
and hey presto! - I had downloaded the full source code hence the size! The non-source app,
Git-1.7.4-preview20110204.exe
was just 13MB via http://code.google.com/p/msysgit/downloads/list?can=3
- 我一直在尝试使用腻子生成密钥对,但无法使其正常工作。最后我意识到我可以通过 msysgit 命令行使用
ssh-keygen -C "[email protected]" -t rsa
. 使用nano /home/git/.ssh/authorized_keys
and hey presto将公钥复制到服务器! - 我已经下载了完整的源代码,因此大小!非源应用程序,
Git-1.7.4-preview20110204.exe
通过http://code.google.com/p/msysgit/downloads/list?can=3只有 13MB
回答by Cercerilla
I'm not sure if this should be here or if it would be best migrated over to a different site, but since I might be able to help I'll go ahead and answer.
我不确定这是否应该在这里,或者是否最好将其迁移到其他站点,但由于我可能能够提供帮助,因此我会继续回答。
I just skimmed the articles you linked. It looks like they both deal with accessing a git server over ssh, which you mention, so that's what I'll focus on.
我只是浏览了你链接的文章。看起来他们都处理通过 ssh 访问 git 服务器,这就是你提到的,所以这就是我要关注的。
First, on your server:
首先,在您的服务器上:
You need to set up an account on the server so that you can log in. This can be either a generic git account, or your own personal account. For the moment we'll assume that you are setting it up to work with a personal account. What you want to do is create your account, then somewhere accessible to that account (say, in your home directory), create the git repository.
您需要在服务器上设置一个帐户,以便您可以登录。这可以是一个通用的 git 帐户,也可以是您自己的个人帐户。目前,我们假设您将其设置为使用个人帐户。您要做的是创建您的帐户,然后在该帐户可访问的某个地方(例如,在您的主目录中)创建 git 存储库。
mkdir myrepo.git
cd myrepo.git
git --bare init --shared=all
So now you have a git repository up on the server. Depending on the git client you are using, you might not need to mess with keys right now. If SSH is configured on your server to allow password login then you can probably just connect and enter your password when you need to interact with the server. If you want to set up keys, what you need to do is to generate an ssh public key. I don't know off hand how to do this in windows, but in linux you'd do something like:
所以现在你在服务器上有一个 git 存储库。根据您使用的 git 客户端,您现在可能不需要弄乱密钥。如果您的服务器上配置了 SSH 以允许密码登录,那么当您需要与服务器交互时,您可能只需连接并输入密码即可。如果要设置密钥,您需要做的是生成一个 ssh 公钥。我不知道如何在 Windows 中执行此操作,但在 linux 中您会执行以下操作:
ssh-keygen -t rsa -b 1024
That command will generate two files, "id_rsa" and "id_rsa.pub"; whatever tool you use should also generate two files, a public key and a private key. They might have different names, but assume for now that "id_rsa.pub" is the name of your public key file.
该命令将生成两个文件,“id_rsa”和“id_rsa.pub”;您使用的任何工具也应该生成两个文件,一个公钥和一个私钥。它们可能有不同的名称,但现在假设“id_rsa.pub”是您的公钥文件的名称。
You want to copy the public key to the server, you can use scp, ftp, or just move it over using a thumbdrive. Either way, once you get it onto the server, and it's accessible by your user, log in as your user on the server. You want to add the public key to your "authorized_hosts" file, so after logging in to your account on the server, do this:
您想将公钥复制到服务器,您可以使用 scp、ftp,或者只是使用拇指驱动器将其移动。无论哪种方式,一旦您将它放到服务器上并且您的用户可以访问它,请在服务器上以您的用户身份登录。您想将公钥添加到您的“authorized_hosts”文件中,因此在登录到您在服务器上的帐户后,执行以下操作:
cd
mkdir .ssh
cat id_rsa.pub >> .ssh/authorized_hosts
rm id_rsa.pub
Now, from your workstation you need to configure your ssh client to use the private key your generated. Again, I don't know how to do this on Windows and it will probably vary depending on what ssh client you are using, so you'll need to get that information somewhere else.
现在,您需要从您的工作站配置您的 ssh 客户端以使用您生成的私钥。同样,我不知道如何在 Windows 上执行此操作,它可能会因您使用的 ssh 客户端而异,因此您需要在其他地方获取该信息。
Next, you need to create your local repository, add some files, and make a commit. Note that you can't clone the remote repository you made yet because there's nothing in there. Once you have some commits made locally, you need to set the remote server in your repository.
接下来,您需要创建本地存储库,添加一些文件并进行提交。请注意,您还不能克隆您创建的远程存储库,因为那里没有任何内容。在本地进行一些提交后,您需要在存储库中设置远程服务器。
If you are using the git command line tools, you can run:
如果您使用的是 git 命令行工具,则可以运行:
git remote add origin user@yourserver:myrepo.git
If you put the repository somewhere other than your home directory, use the full path:
如果您将存储库放在主目录以外的其他位置,请使用完整路径:
git remote add origin user@yourserver:/path/to/repo.git
Note that you need the ".git" in there, since your directory name has ".git" as part of the name.
请注意,您需要在其中包含“.git”,因为您的目录名称包含“.git”作为名称的一部分。
If you are using a GUI tool, then you instead just edit the configuration file for the repository. This will be in the top level of your repository under ".git/config". You'll want to add something like
如果您使用的是 GUI 工具,那么您只需编辑存储库的配置文件。这将位于“.git/config”下存储库的顶层。你会想要添加类似的东西
[remote "origin"]
url = user@yourserver:/path/to/repo.git
fetch = +refs/heads/*:refs/remotes/origin/*
Now that your remote is configured, and you have some commits locally, you can push your master branch up to the server. If you're using the command line use:
现在您的远程已配置,并且您在本地有一些提交,您可以将您的 master 分支推送到服务器。如果您使用命令行,请使用:
git push origin master
Or, if you're working on a different branch:
或者,如果您在不同的分支上工作:
git push origin mybranch
If you are using a GUI frontend for get then you'll need to look up the documentation for that tool on how to push.
如果您使用 GUI 前端来获取,那么您需要查找该工具的文档,了解如何推送。
回答by iankits
I would like to recommend to use Gitolitethat allows to setup git hosting on central srever with great features like access control and creating/managin repositories etc.
我想推荐使用Gitolite,它允许在中央服务器上设置 git 托管,具有访问控制和创建/管理存储库等强大功能。
And for windows, the tool called git-scmcan be used for setting up git client and also have GUI panel for git repositories. After installation of this git tool, you will get a git bashtool and a git GUI.
对于 Windows,名为git-scm的工具可用于设置 git 客户端,并且还具有用于 git 存储库的 GUI 面板。安装这个 git 工具后,你会得到一个git bash工具和一个git GUI。
Gitolitemakes settting up of a git hosting server and client very simple.
Gitolite使 git 托管服务器和客户端的设置变得非常简单。
I hope this may help new people who are looking for an efficient solution for settting up git hosting server and clients in some easy to understand simple steps.
我希望这可以帮助正在寻找有效解决方案以通过一些易于理解的简单步骤设置 git 托管服务器和客户端的新人。
回答by Alfonso
I followed the most voted answer and I didnt get it to work. I found this tutorial: https://www.digitalocean.com/community/tutorials/how-to-set-up-ssh-keys--2and I got it working. I think the problem was that, in the server, the public key is to be copied to a file "authorized_keys" and not "authorized_hosts".
我遵循了投票最多的答案,但我没有让它起作用。我找到了这个教程:https: //www.digitalocean.com/community/tutorials/how-to-set-up-ssh-keys--2并且我让它工作了。我认为问题在于,在服务器中,公钥将被复制到文件“authorized_keys”而不是“authorized_hosts”。