简体   繁体   English

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

[英]How to create a remote Git repository from a local one?

I have a local Git repository.我有一个本地 Git 存储库。 I would like to make it available on a remote, ssh-enabled, server.我想让它在支持 ssh 的远程服务器上可用。 How do I do this?我该怎么做呢?

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 . 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.现在任何其他存储库都可以从远程存储库中pull

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.为了初始设置任何 Git 服务器,您必须将现有存储库导出到新的裸存储库 - 一个不包含工作目录的存储库。 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.为了克隆存储库以创建新的裸存储库,请使用--bare选项运行克隆命令。 By convention, bare repository directories end in .git , like so:按照惯例,裸存储库目录以.git ,如下所示:

$ 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.此命令单独获取 Git 存储库,没有工作目录,并专门为它单独创建一个目录。

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. 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 user@git.example.com:/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此时,具有/opt/git访问同一服务器的其他用户可以通过运行克隆您的存储库

$ git clone user@git.example.com:/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.如果用户通过 SSH 连接到服务器并且对/opt/git/my_project.git目录具有写入权限,他们也将自动拥有推送权限。 Git will automatically add group write permissions to a repository properly if you run the git init command with the --shared option.如果您使用--shared选项运行 git init 命令,Git 将自动将组写入权限正确添加到存储库。

$ ssh user@git.example.com
$ 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.获取 Git 存储库非常容易,创建一个裸版本,并将其放置在您和您的合作者可以访问 SSH 的服务器上。 Now you're ready to collaborate on the same project.现在您已准备好在同一个项目上进行协作。

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.给在 Windows 上创建本地副本并希望在 Unix 行系统上创建相应的远程存储库的人的说明,其中文本文件在类 Unix 系统上的开发人员进一步克隆时获得 LF 结尾,但在 Windows 上获得 CRLF 结尾。

If you created your Windows repository before setting up line-ending translation then you have a problem.如果您在设置行尾翻译之前创建了 Windows 存储库,那么您就会遇到问题。 Git's default setting is no translation, so your working set uses CRLF but your repository (ie the data stored under.git) has saved the files as CRLF too. Git 的默认设置是不翻译,因此您的工作集使用 CRLF,但您的存储库(即存储在 .git 下的数据)也将文件保存为 CRLF。

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.您最终会在类 Unix 存储库中使用 CRLF,这不是您想要的。

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 .要在远程存储库中获取 LF,您必须首先确保 LF 在本地存储库中,方法是重新规范化您的 Windows 存储库 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.这对您的 Windows 工作集没有明显影响,它仍然具有 CRLF 结尾,但是当您推送到遥控器时,遥控器将正确获得 LF。

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).我不确定是否有一种简单的方法来判断 Windows 存储库中的行尾 - 我想你可以通过设置 core.autocrlf=false 然后克隆来测试它(如果 repo 有 LF 结尾,克隆将有LF 也是)。

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 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:然后 git 使用以下关系在“original_repo”中设置配置:

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 (从目录 original_repo 中)cd .. git 克隆 --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.然后 'my_remote.git' 以它的配置结束,其中 'origin' 指向作为远程的 'original_repo',remote.origin.url 等同于本地目录路径,如果要移动它可能不合适到服务器。

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).虽然“远程”引用在不合适的情况下很容易在以后删除,但仍然必须将“original_repo”设置为指向“my_remote.git”作为上游远程(或指向它要去的任何地方)分享自)。 So technically, you can arrive at the same result with a few more steps with approach #2.因此,从技术上讲,您可以通过方法 #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.但是#1似乎是一种更直接的方法来创建源自本地的“中央裸共享存储库”,适合移动到服务器,涉及的步骤更少。 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.警告:我通过使用真实存储库在本地系统上进行测试,然后在结果之间进行逐个文件比较来了解上述内容(在 2019 年 8 月上旬撰写本文时)。 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.但我的测试帮助我得出结论,#1 是我目前首选的方法。

A remote repository is generally a bare repository — a Git repository that has no working directory.远程仓库通常是一个裸仓库——一个没有工作目录的 Git 仓库。 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.这只是 Git 数据。 In the simplest terms, a bare repository is the contents of your project's.git directory and nothing else.用最简单的术语来说,裸存储库是项目的 .git 目录的内容,仅此而已。

You can make a bare git repository with the following code:您可以使用以下代码创建一个裸 git 存储库:

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

One options for having a remote git repository is using SSH protocol:拥有远程 git 存储库的一种选择是使用 SSH 协议:

A common transport protocol for Git when self-hosting is over SSH.自托管超过 SSH 时 Git 的通用传输协议。 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 已经在大多数地方设置了对服务器的访问权限——如果没有设置,也很容易做到。 SSH is also an authenticated network protocol and, because it's ubiquitous, it's generally easy to set up and use. SSH 也是一种经过身份验证的网络协议,由于它无处不在,因此通常易于设置和使用。

To clone a Git repository over SSH, you can specify an ssh:// URL like this:要通过 SSH 克隆 Git 存储库,您可以指定ssh:// ZE6B391A8D2C4D45702A23

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

Or you can use the shorter scp-like syntax for the SSH protocol:或者,您可以对 SSH 协议使用较短的类似 scp 的语法:

 $ 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.在上述两种情况下,如果您不指定可选用户名,Git 会假定您当前登录的用户是该用户。

The Pros优点

The pros of using SSH are many.使用 SSH 的优点很多。 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.首先,SSH 相对容易设置 — SSH 守护程序很常见,许多网络管理员都有使用它们的经验,并且许多操作系统发行版都使用它们设置或具有管理它们的工具。 Next, access over SSH is secure — all data transfer is encrypted and authenticated.接下来,通过 SSH 访问是安全的——所有数据传输都经过加密和验证。 Last, like the HTTPS, Git and Local protocols, SSH is efficient, making the data as compact as possible before transferring it.最后,与 HTTPS、Git 和本地协议一样,SSH 是高效的,可以在传输之前使数据尽可能紧凑。

The Cons缺点

The negative aspect of SSH is that it doesn't support anonymous access to your Git repository. SSH 的不利方面是它不支持匿名访问您的 Git 存储库。 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.如果您使用的是 SSH,人们必须对您的机器具有 SSH 访问权限,即使是只读容量,这也不会使 SSH 有利于您的人们检查它的源代码项目以进行克隆。 If you're using it only within your corporate network, SSH may be the only protocol you need to deal with.如果您仅在公司网络中使用它,SSH 可能是您需要处理的唯一协议。 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.如果您想允许匿名只读访问您的项目并且还想使用 SSH,则必须设置 SSH 以便您推送,但其他人可以从中获取。

For more information, check the reference: Git on the Server - The Protocols有关更多信息,请查看参考: 服务器上的 Git - 协议

You need to create a directory on a remote server.您需要在远程服务器上创建一个目录。 Then use "git init" command to set it as a repository.然后使用“git init”命令将其设置为存储库。 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. 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在这里检查 - https://github.com/skbobade/ocgi

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

I have a raspberry where I can access via ssh through public key (no prompt for password).我有一个覆盆子,我可以通过公钥通过 ssh 访问(不提示输入密码)。

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就是这样

if you want hnow connect,you can git -T your git url。如果你想知道连接,你可以 git -T 你的 git url。

Normally you can set up a git repo by just using the init command通常,您只需使用init命令即可设置 git 存储库

git init

In your case, there is already a repo on a remote available.在你的情况下,已经有一个远程可用的 repo。 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:取决于您访问远程仓库的方式(使用 url 中的用户名或处理验证的 ssh 密钥)仅使用clone命令:

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

There are also other ways to clone the repo.还有其他方法可以克隆 repo。 This way you call it if you have a ssh key setup on your machine which verifies on pulling your repository.如果您的机器上有一个 ssh 密钥设置,您可以通过这种方式调用它,以验证拉取您的存储库。 There are other combinations of the url if you want to include your password and username inside to login into your remote repository.如果您想在其中包含密码和用户名以登录到远程存储库,还有 url 的其他组合。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM