我有一个本地Git存储库。我想让它在远程的、启用ssh的服务器上可用。我怎么做呢?


当前回答

对于在Windows上创建本地副本并希望在unix行系统上创建相应远程存储库的人来说,这里的文本文件在类unix系统上的开发人员进一步克隆时得到LF结尾,但在Windows上得到CRLF结尾。

如果你在设置行尾转换之前创建了Windows存储库,那么你就有问题了。Git的默认设置是不翻译,所以您的工作集使用CRLF,但您的存储库(即存储在. Git下的数据)也将文件保存为CRLF。

当您推到远程时,保存的文件将按原样复制,不会发生行尾转换。(行尾转换发生在文件提交到存储库时,而不是推送存储库时)。您最终会在类unix存储库中使用CRLF,这不是您想要的。

为了在远程存储库中获得LF,您必须首先通过重新规范化您的Windows存储库来确保LF在本地存储库中。这将对您的Windows工作集没有明显的影响,它仍然有CRLF结束,但是当您推到远程时,远程将正确地获得LF。

我不确定是否有一种简单的方法来告诉你在你的Windows存储库中有什么行尾-我猜你可以通过设置core来测试它。selflf =false,然后克隆(如果回购有LF结尾,克隆也会有LF结尾)。

其他回答

对于在Windows上创建本地副本并希望在unix行系统上创建相应远程存储库的人来说,这里的文本文件在类unix系统上的开发人员进一步克隆时得到LF结尾,但在Windows上得到CRLF结尾。

如果你在设置行尾转换之前创建了Windows存储库,那么你就有问题了。Git的默认设置是不翻译,所以您的工作集使用CRLF,但您的存储库(即存储在. Git下的数据)也将文件保存为CRLF。

当您推到远程时,保存的文件将按原样复制,不会发生行尾转换。(行尾转换发生在文件提交到存储库时,而不是推送存储库时)。您最终会在类unix存储库中使用CRLF,这不是您想要的。

为了在远程存储库中获得LF,您必须首先通过重新规范化您的Windows存储库来确保LF在本地存储库中。这将对您的Windows工作集没有明显的影响,它仍然有CRLF结束,但是当您推到远程时,远程将正确地获得LF。

我不确定是否有一种简单的方法来告诉你在你的Windows存储库中有什么行尾-我猜你可以通过设置core来测试它。selflf =false,然后克隆(如果回购有LF结尾,克隆也会有LF结尾)。

为了最初设置任何Git服务器,您必须将现有的存储库导出到一个新的裸存储库—一个不包含工作目录的存储库。这通常很简单。为了克隆存储库以创建一个新的裸存储库,可以运行带有——bare选项的clone命令。按照惯例,裸存储库目录以.git结尾,如下所示:

$ git clone --bare my_project my_project.git
Initialized empty Git repository in /opt/projects/my_project.git/

这个命令单独获取Git存储库,没有工作目录,并单独为它创建一个目录。

现在您已经有了存储库的一个裸副本,您所需要做的就是将它放到服务器上并设置协议。假设您已经设置了一个名为git.example.com的服务器,您可以通过SSH访问该服务器,并且您希望将所有Git存储库存储在/opt/ Git目录下。你可以通过复制你的裸存储库来建立新的存储库:

$ scp -r my_project.git user@git.example.com:/opt/git

此时,对同一台服务器具有读/opt/git目录访问权限的其他用户可以通过运行命令克隆您的存储库

$ git clone user@git.example.com:/opt/git/my_project.git

如果一个用户ssh到一个服务器,并且对/opt/git/my_project有写权限。Git目录下,他们也会自动拥有推送访问权限。如果你使用——shared选项运行Git init命令,Git会自动为存储库添加组写权限。

$ ssh user@git.example.com
$ cd /opt/git/my_project.git
$ git init --bare --shared

获取Git存储库,创建一个裸版本,并将其放在您和您的合作者可以SSH访问的服务器上,这是非常容易的。现在你已经准备好在同一个项目上合作了。

远程存储库通常是一个裸存储库——Git存储库 没有工作目录。因为存储库仅用于 对于协作点,没有理由检查快照 在磁盘上;它只是Git数据。用最简单的话来说,就是赤裸 Repository是项目的.git目录的内容 什么都没有。

你可以用下面的代码创建一个git仓库:

$ git clone --bare /path/to/project project.git

有一个远程git存储库的选项是使用SSH协议:

A common transport protocol for Git when self-hosting is over SSH. This is because SSH access to servers is already set up in most places — and if it isn’t, it’s easy to do. SSH is also an authenticated network protocol and, because it’s ubiquitous, it’s generally easy to set up and use. To clone a Git repository over SSH, you can specify an ssh:// URL like this: $ git clone ssh://[user@]server/project.git Or you can use the shorter scp-like syntax for the SSH protocol: $ git clone [user@]server:project.git In both cases above, if you don’t specify the optional username, Git assumes the user you’re currently logged in as. The Pros The pros of using SSH are many. First, SSH is relatively easy to set up — SSH daemons are commonplace, many network admins have experience with them, and many OS distributions are set up with them or have tools to manage them. Next, access over SSH is secure — all data transfer is encrypted and authenticated. Last, like the HTTPS, Git and Local protocols, SSH is efficient, making the data as compact as possible before transferring it. The Cons The negative aspect of SSH is that it doesn’t support anonymous access to your Git repository. If you’re using SSH, people must have SSH access to your machine, even in a read-only capacity, which doesn’t make SSH conducive to open source projects for which people might simply want to clone your repository to examine it. If you’re using it only within your corporate network, SSH may be the only protocol you need to deal with. If you want to allow anonymous read-only access to your projects and also want to use SSH, you’ll have to set up SSH for you to push over but something else for others to fetch from.

欲了解更多信息,请查看参考文献: 服务器上的Git -协议

在当前代码文件夹中。

git remote add origin http://yourdomain-of-git.com/project.git
git push --set-upstream origin master

然后通过

git remote --v

我有一个覆盆子,我可以通过ssh通过公钥访问(没有提示密码)。

在覆盆子上,我做到了

mkdir -p /home/pi/my/repo
cd /home/pi/my/repo
git init --bare

在我的笔记本电脑上,我做到了

git clone  ssh://pi@raspberry/home/pi/my/repo
cd myrepo
touch README.md
git add README.md
git commit -m "First commit"
git push

就是这样