如何从本地创建远程 Git 存储库?

发布于 2024-11-19 10:24:57 字数 51 浏览 3 评论 0原文

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

I have a local Git repository. I would like to make it available on a remote, ssh-enabled, server. How do I do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

鱼窥荷 2024-11-26 10:24:57

我认为您在远程端创建了一个裸存储库,git init --bare,将远程端添加为本地存储库的推/拉跟踪器(git remote add origin URL code>),然后在本地你只需说git push origin master。现在任何其他存储库都可以从远程存储库中

I think you make a bare repository on the remote side, git init --bare, add the remote side as the push/pull tracker for your local repository (git remote add origin URL), and then locally you just say git push origin master. Now any other repository can pull from the remote repository.

℡Ms空城旧梦 2024-11-26 10:24:57

为了最初设置任何 Git 服务器,您必须将现有存储库导出到新的裸存储库 - 不包含工作目录的存储库。这通常很容易做到。为了克隆存储库以创建新的裸存储库,请使用 --bare 选项运行克隆命令。按照惯例,裸存储库目录以 .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 [email protected]:/opt/git

此时,对同一服务器具有 SSH 访问权限且对 /opt/git 目录具有读取访问权限的其他用户可以克隆您的存储库。 通过 SSH 连接

$ git clone [email protected]:/opt/git/my_project.git

如果用户 到服务器并具有对 /opt/git/my_project.git 目录的写访问权限,他们也将自动具有推送访问权限。如果您使用 --shared 选项运行 git init 命令,Git 将自动正确地向存储库添加组写入权限。

$ ssh [email protected]
$ cd /opt/git/my_project.git
$ git init --bare --shared

获取 Git 存储库、创建裸版本并将其放置在您和您的协作者可以通过 SSH 访问的服务器上非常容易。现在您已准备好在同一个项目上进行协作。

In order to initially set up any Git server, you have to export an existing repository into a new bare repository — a repository that doesn’t contain a working directory. This is generally straightforward to do. In order to clone your repository to create a new bare repository, you run the clone command with the --bare option. By convention, bare repository directories end in .git, like so:

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

This command takes the Git repository by itself, without a working directory, and creates a directory specifically for it alone.

Now that you have a bare copy of your repository, all you need to do is put it on a server and set up your protocols. Let’s say you’ve set up a server called git.example.com that you have SSH access to, and you want to store all your Git repositories under the /opt/git directory. You can set up your new repository by copying your bare repository over:

$ scp -r my_project.git [email protected]:/opt/git

At this point, other users who have SSH access to the same server which has read-access to the /opt/git directory can clone your repository by running

$ git clone [email protected]:/opt/git/my_project.git

If a user SSHs into a server and has write access to the /opt/git/my_project.git directory, they will also automatically have push access. Git will automatically add group write permissions to a repository properly if you run the git init command with the --shared option.

$ ssh [email protected]
$ cd /opt/git/my_project.git
$ git init --bare --shared

It is very easy to take a Git repository, create a bare version, and place it on a server to which you and your collaborators have SSH access. Now you’re ready to collaborate on the same project.

诗化ㄋ丶相逢 2024-11-26 10:24:57

上面两种流行的解决方案之间有一个有趣的区别:

  1. 如果您像这样创建裸存储库:

    cd /outside_of_any_repo
    mkdir my_remote.git
    cd my_remote.git
    git init --裸
    

然后

cd  /your_path/original_repo
git remote add origin /outside_of_any_repo/my_remote.git
git push --set-upstream origin master

git 在“original_repo”中设置具有以下关系的配置:

original_repo origin --> /outside_of_any_repo/my_remote.git/

后者作为上游远程。并且上游遥控器的配置中没有任何其他遥控器。

  1. 但是,如果您以相反的方式进行操作:

    (来自目录original_repo)
    光盘 ..
    git clone --bareoriginal_repo/outside_of_any_repo/my_remote.git
    

那么“my_remote.git”最终的配置是“origin”作为远程指向“original_repo”,而remote.origin.url相当于本地目录路径,如果要移动到服务器,该路径可能不合适。

虽然“远程”引用如果不合适的话很容易在以后删除,但仍然必须将“original_repo”设置为指向“my_remote.git”作为上游远程(或指向它要去的任何地方)分享自)。因此从技术上讲,您可以使用方法 #2 通过更多步骤获得相同的结果。但#1 似乎是一种更直接的方法来创建源自本地的“中央裸共享存储库”,适合移动到服务器,涉及的步骤更少。我认为这取决于您希望远程存储库扮演的角色。 (是的,这与文档 此处。)

警告:我通过使用真实的存储库在本地系统上进行测试,然后逐个文件地进行测试,了解了上述内容(在 2019 年 8 月上旬撰写本文时)结果之间的比较。但!我还在学习中,所以可能有更正确的方法。但我的测试帮助我得出结论:#1 是我目前首选的方法。

There is an interesting difference between the two popular solutions above:

  1. If you create the bare repository like this:

    cd     /outside_of_any_repo
    mkdir  my_remote.git
    cd     my_remote.git
    git init --bare
    

and then

cd  /your_path/original_repo
git remote add origin /outside_of_any_repo/my_remote.git
git push --set-upstream origin master

Then git sets up the configuration in 'original_repo' with this relationship:

original_repo origin --> /outside_of_any_repo/my_remote.git/

with the latter as the upstream remote. And the upstream remote doesn't have any other remotes in its configuration.

  1. However, if you do it the other way around:

    (from in directory original_repo)
    cd ..
    git clone --bare original_repo  /outside_of_any_repo/my_remote.git
    

then 'my_remote.git' winds up with its configuration having 'origin' pointing back to 'original_repo' as a remote, with a remote.origin.url equating to local directory path, which might not be appropriate if it is going to be moved to a server.

While that "remote" reference is easy to get rid of later if it isn't appropriate, 'original_repo' still has to be set up to point to 'my_remote.git' as an up-stream remote (or to wherever it is going to be shared from). So technically, you can arrive at the same result with a few more steps with approach #2. But #1 seems a more direct approach to creating a "central bare shared repo" originating from a local one, appropriate for moving to a server, with fewer steps involved. I think it depends on the role you want the remote repo to play. (And yes, this is in conflict with the documentation here.)

Caveat: I learned the above (at this writing in early August 2019) by doing a test on my local system with a real repo, and then doing a file-by-file comparison between the results. But! I am still learning, so there could be a more correct way. But my tests have helped me conclude that #1 is my currently-preferred method.

素衣风尘叹 2024-11-26 10:24:57

对于那些在 Windows 上创建本地副本并希望在 Unix 系统上创建相应的远程存储库的人来说,请注意,其中文本文件在类 Unix 系统上的开发人员进一步克隆时以 LF 结尾,但在 Windows 上以 CRLF 结尾。

如果您在设置行结束翻译之前创建了 Windows 存储库,那么您就会遇到问题。 Git 的默认设置是不翻译,因此您的工作集使用 CRLF,但您的存储库(即存储在 .git 下的数据)也将文件保存为 CRLF。

当您推送到远程时,保存的文件将按原样复制,不会发生行结束翻译。 (行结束翻译发生在文件提交到存储库时,而不是推送存储库时)。您最终会在类 Unix 存储库中得到 CRLF,这不是您想要的。

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

我不确定是否有一种简单的方法可以告诉您 Windows 存储库中的行结尾 - 我想您可以通过设置 core.autocrlf=false 然后克隆来测试它(如果存储库有 LF 结尾,则克隆将具有LF 也)。

A note for people who created the local copy on Windows and want to create a corresponding remote repository on a Unix-line system, where text files get LF endings on further clones by developers on Unix-like systems, but CRLF endings on Windows.

If you created your Windows repository before setting up line-ending translation then you have a problem. Git's default setting is no translation, so your working set uses CRLF but your repository (i.e. the data stored under .git) has saved the files as CRLF too.

When you push to the remote, the saved files are copied as-is, no line ending translation occurs. (Line ending translation occurs when files are commited to a repository, not when repositories are pushed). You end up with CRLF in your Unix-like repository, which is not what you want.

To get LF in the remote repository you have to make sure LF is in the local repository first, by re-normalizing your Windows repository. This will have no visible effect on your Windows working set, which still has CRLF endings, however when you push to remote, the remote will get LF correctly.

I'm not sure if there's an easy way to tell what line endings you have in your Windows repository - I guess you could test it by setting core.autocrlf=false and then cloning (If the repo has LF endings, the clone will have LF too).

帅气尐潴 2024-11-26 10:24:57

远程存储库通常是一个裸存储库 - Git 存储库
没有工作目录。因为存储库仅用作
协作点,没有理由检查快照
出到磁盘上;这只是 Git 数据。用最简单的话来说,就是一个裸
存储库是项目的 .git 目录的内容,
没有别的。

您可以使用以下代码创建一个裸 git 存储库:

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

拥有远程 git 存储库的一种选择是使用 SSH 协议:

当通过 SSH 自托管时,Git 的通用传输协议。
这是因为大多数服务器已经设置了 SSH 访问
地方——如果不是,也很容易做到。 SSH 也是一个
经过身份验证的网络协议,并且因为它无处不在,所以它是
通常易于设置和使用。

要通过 SSH 克隆 Git 存储库,您可以指定 ssh:// URL
像这样:

$ git clone ssh://[user@]server/project.git

或者您可以对 SSH 协议使用更短的类似 scp 的语法:

$ git clone [user@]server:project.git

在上述两种情况下,如果您不指定可选用户名,Git
假定您当前登录的用户。

优点

使用 SSH 的优点有很多。一、SSH比较容易设置
up — SSH 守护进程很常见,许多网络管理员都有经验
与它们一起,并且许多操作系统发行版都是与它们一起设置的或具有
管理它们的工具。接下来,通过 SSH 的访问是安全的——所有数据
传输经过加密和验证。最后,像 HTTPS、Git 和
本地协议,SSH高效,使得数据紧凑如
可以在传输之前进行。

缺点

SSH 的缺点是它不支持匿名访问
到您的 Git 存储库。如果您使用 SSH,那么人们必须拥有 SSH
访问您的计算机,即使是只读容量,这并不
使 SSH 有利于人们可能参与的开源项目
只是想克隆您的存储库来检查它。如果你正在使用它
仅在您的公司网络内,SSH 可能是您唯一的协议
需要处理。如果您想允许匿名只读访问
您的项目并且还想使用 SSH,您必须为
你可以推倒,但其他人可以从中获取其他东西。

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

A remote repository is generally a bare repository — a Git repository
that has no working directory. Because the repository is only used as
a collaboration point, there is no reason to have a snapshot checked
out on disk; it’s just the Git data. In the simplest terms, a bare
repository is the contents of your project’s .git directory and
nothing else.

You can make a bare git repository with the following code:

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

One options for having a remote git repository is using SSH protocol:

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.

For more information, check the reference:
Git on the Server - The Protocols

此刻的回忆 2024-11-26 10:24:57

在当前代码文件夹中。

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

然后通过审核

git remote --v

In current code folder.

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

Then review by

git remote --v
獨角戲 2024-11-26 10:24:57

您需要在远程服务器上创建一个目录。然后使用“git init”命令将其设置为存储库。应该为您拥有的每个新项目(每个新文件夹)执行此操作

假设您已经使用 ssh 密钥设置并使用了 git,我编写了一个小的 Python 脚本,当从工作目录执行时,它将设置远程并初始化目录作为 git 存储库。当然,您必须编辑脚本(仅一次)来告诉它所有存储库的服务器和根路径。

检查此处 - https://github.com/skbobade/ocgi

You need to create a directory on a remote server. Then use "git init" command to set it as a repository. This should be done for each new project you have (each new folder)

Assuming you have already setup and used git using ssh keys, I wrote a small Python script, which when executed from a working directory will set up a remote and initialize the directory as a git repo. Of course, you will have to edit script (only once) to tell it server and Root path for all repositories.

Check here - https://github.com/skbobade/ocgi

只为守护你 2024-11-26 10:24:57

我有一个树莓派,我可以通过公钥通过 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

就是这样

I have a raspberry where I can access via ssh through public key (no prompt for password).

On the raspberry I did

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

On my laptop I did

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

And that was it

§对你不离不弃 2024-11-26 10:24:57

通常,您只需使用 init 命令即可设置 git 存储库

git init

。在您的情况下,远程可用的存储库已经存在。根据您访问远程存储库的方式(在 url 中使用用户名或处理验证的 ssh 密钥),仅使用 clone 命令:

git clone git@[my.url.com]:[git-repo-name].git

还有其他方法来克隆存储库。如果您的计算机上设置了 ssh 密钥来验证拉取存储库,则可以通过这种方式调用它。如果您想在其中包含密码和用户名以登录远程存储库,则还有其他 url 组合。

Normally you can set up a git repo by just using the init command

git init

In your case, there is already a repo on a remote available. Dependent on how you access your remote repo ( with username inside the url or a ssh key which handles verification ) use just the clone command:

git clone git@[my.url.com]:[git-repo-name].git

There are also other ways to clone the repo. This way you call it if you have a ssh key setup on your machine which verifies on pulling your repository. There are other combinations of the url if you want to include your password and username inside to login into your remote repository.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文