如何连接到远程 Git 存储库?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20291731/
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 connect to a remote Git repository?
提问by Mateus Viccari
I am working with a team and we want to use Git (Not with GitHub, we have a private remote machine). We were using SVN until now. We have a remote machine that works like an SVN repository. Now, we want to make this machine into the Git central repository. By what I have read, what everyone needs to do on their machines, is to install Git and use the following command to add the remote master repository:
我正在与一个团队合作,我们想使用 Git(不是使用 GitHub,我们有一台私人远程机器)。到目前为止,我们一直在使用 SVN。我们有一台像 SVN 存储库一样工作的远程机器。现在,我们要将这台机器变成 Git 中央存储库。据我所知,每个人都需要在他们的机器上做的是安装 Git 并使用以下命令添加远程主存储库:
git remote add origin <remote_repo_url>
But what is the remote_repo_url
? How can I figure it out?
Our remote machine have IPs like 189.14.666.666, in the remote machine I've installed Git and created a repository under C:\MY_GIT_REPOSITORY. What should I put in the URL in the local machines?
但什么是remote_repo_url
?我怎样才能弄清楚?我们的远程机器有像 189.14.666.666 这样的 IP,在远程机器上我安装了 Git 并在 C:\MY_GIT_REPOSITORY 下创建了一个存储库。我应该在本地机器的 URL 中输入什么?
采纳答案by DusteD
Now, if the repository is already existing on a remote machine, and you do not have anything locally, you do git clone instead.
现在,如果存储库已经存在于远程机器上,而您在本地没有任何东西,您可以使用 git clone 代替。
The URL format is simple, it is PROTOCOL:/[user@]remoteMachineAddress/path/to/repository.git
URL格式很简单,就是PROTOCOL:/[user@]remoteMachineAddress/path/to/repository.git
For example, cloning a repository on a machine to which you have SSH access using the "dev" user, residing in /srv/repositories/awesomeproject.git and that machine has the ip 10.11.12.13 you do:
例如,使用“dev”用户在具有 SSH 访问权限的机器上克隆存储库,该机器位于 /srv/repositories/awesomeproject.git 并且该机器具有您执行的 ip 10.11.12.13:
git clone ssh://[email protected]/srv/repositories/awesomeproject.git
回答by Bijendra
It's simple and follow the small Steps to proceed:
这很简单,请按照以下小步骤继续操作:
- Install git on the remote server say some ec2 instance
- Now create a project folder `$mkdir project.git
$cd project and execute $git init --bare
- 在远程服务器上安装 git 说一些 ec2 实例
- 现在创建一个项目文件夹`$mkdir project.git
$cd project and execute $git init --bare
Let's say this project.git folder is present at your ip with address inside home_folder/workspace/project.git
, forex- ec2 - /home/ubuntu/workspace/project.git
假设这个 project.git 文件夹存在于您的 ip 中,地址在里面home_folder/workspace/project.git
,forex- ec2 - /home/ubuntu/workspace/project.git
Now in your local machine, $cd
into the project folder which you want to push to git execute the below commands:
现在在您的本地机器中,$cd
进入您要推送到 git 的项目文件夹,执行以下命令:
git init .
git remote add origin [email protected]:/home/ubuntu/workspace/project.git
git add .
git commit -m "Initial commit"
git init .
git remote add origin [email protected]:/home/ubuntu/workspace/project.git
git add .
git commit -m "Initial commit"
Below is an optional command but found it has been suggested as i was working to setup the same thing
下面是一个可选命令,但发现它已被建议,因为我正在努力设置相同的东西
git config --global remote.origin.receivepack "git receive-pack"
git config --global remote.origin.receivepack "git receive-pack"
git pull origin master
git push origin master
git pull origin master
git push origin master
This should work fine and will push the local code to the remote git repository.
这应该可以正常工作,并将本地代码推送到远程 git 存储库。
To check the remote fetch url, cd project_folder/.git
and cat config
, this will give the remote url being used for pull and push operations.
要检查远程获取 urlcd project_folder/.git
和cat config
,这将提供用于拉取和推送操作的远程 url。
You can also use an alternative way, after creating the project.git
folder on git, clone the project and copy the entire content into that folder. Commit the changes and it should be the same way. While cloning make sure you have access or the key being is the secret key for the remote server being used for deployment.
您也可以使用另一种方式,project.git
在 git 上创建文件夹后,克隆项目并将整个内容复制到该文件夹中。提交更改,它应该是相同的方式。克隆时请确保您有权访问或密钥是用于部署的远程服务器的密钥。
回答by Mohammad AbuShady
Like you said remote_repo_url
is indeed the IP of the server, and yes it needs to be added on each PC, but it's easier to understand if you create the server first then ask each to clone it.
就像你说remote_repo_url
的确实是服务器的IP,是的,它需要添加到每台PC上,但如果你先创建服务器,然后让每个人克隆它,就更容易理解了。
There's several ways to connect to the server, you can use ssh, http, or even a network drive, each has it's pros and cons. You can refer to the documentationabout protocols and how to connect to the server
有多种方式连接到服务器,您可以使用 ssh、http,甚至网络驱动器,每种方式都有其优缺点。您可以参考有关协议以及如何连接到服务器的文档
You can check the rest of chapter 4for more detailed information, as it's talking about how to set up your own server
您可以查看第 4 章的其余部分以获取更多详细信息,因为它在讨论如何设置您自己的服务器
回答by Arenhag
To me it sounds like the simplest way to expose your git repository on the server (which seems to be a Windows machine) would be to share it as a network resource.
对我来说,在服务器(似乎是 Windows 机器)上公开 git 存储库的最简单方法是将其作为网络资源共享。
Right click the folder "MY_GIT_REPOSITORY" and select "Sharing". This will give you the ability to share your git repository as a network resource on your local network. Make sure you give the correct users the ability to write to that share (will be needed when you and your co-workers push to the repository).
右键单击文件夹“MY_GIT_REPOSITORY”并选择“共享”。这将使您能够将您的 git 存储库作为本地网络上的网络资源共享。确保您为正确的用户提供了写入该共享的能力(当您和您的同事推送到存储库时将需要)。
The URL for the remote that you want to configure would probably end up looking something like
file://\\\\189.14.666.666\MY_GIT_REPOSITORY
您要配置的遥控器的 URL 最终可能看起来像
file://\\\\189.14.666.666\MY_GIT_REPOSITORY
If you wish to use any other protocol (e.g. HTTP, SSH) you'll have to install additional server software that includes servers for these protocols. In lieu of these the file sharing method is probably the easiest in your case right now.
如果您希望使用任何其他协议(例如 HTTP、SSH),您必须安装额外的服务器软件,其中包括这些协议的服务器。代替这些文件共享方法可能是目前最简单的方法。