git clone 从本地到远程

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4142936/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 09:26:40  来源:igfitidea点击:

git clone from local to remote

gitsshdeploymentremote-server

提问by hlidotbe

We're in the process of migrating from Mercurial to Git for our workflow and I have two minor issues.

我们正在为我们的工作流程从 Mercurial 迁移到 Git,我有两个小问题。

First, is it possible to "clone" a local repository directly into an empty remote (ssh) dir?

首先,是否可以将本地存储库直接“克隆”到空的远程 (ssh) 目录中?

Currently when we create a new website we basically clone our CMS locally, configure it and then we clone it on the central repo and on the webserver (hg clone . ssh://account@server/www). That way we have instant access to push/pull goodness.

目前,当我们创建一个新网站时,我们基本上是在本地克隆我们的 CMS,配置它,然后我们将它克隆到中央存储库和网络服务器 ( hg clone . ssh://account@server/www) 上。这样我们就可以即时访问推/拉优点。

This brings me to the second issue, remote deployment.

这让我想到了第二个问题,远程部署。

Currently with Mercurial, I have a simple hooks in the remote repos that execute hg upwhen a changeset is received.

目前使用 Mercurial,我在远程存储库中有一个简单的挂钩,可在hg up接收到变更集时执行。

To do the same with Git I've followed the instructions here: http://caiustheory.com/automatically-deploying-website-from-remote-git-repositorybut I'd like to keep the .git directory in the website root as it is the case with Mercurial (it's protected by Apache config and I can't export GIT_DIR for all accounts as some have more than one website/repos).

要对 Git 执行相同操作,我已按照此处的说明进行操作:http: //caiustheory.com/automatically-deploying-website-from-remote-git-repository但我想将 .git 目录保留在网站根目录中就像 Mercurial 的情况一样(它受 Apache 配置保护,我无法为所有帐户导出 GIT_DIR,因为有些帐户有多个网站/存储库)。

Is it possible to have basically the same setup without separating the working dir from the repos?

是否可以在不将工作目录与存储库分开的情况下进行基本相同的设置?

回答by cdhowie

To answer your first question, yes, you can. Suppose the remote directory is ssh://user@host/home/user/repo. This must be a git repository, create that with git init --bareor scpyour local repo.git(can be created with git clone) directory to remote. Then do:

要回答你的第一个问题,是的,你可以。假设远程目录是ssh://user@host/home/user/repo. 这必须是一个 git 存储库,创建它git init --bare或者scp你的本地repo.git(可以创建git clone)目录到远程。然后做:

git remote add origin ssh://user@host/home/user/repo
git push --all origin

This will push all locally-existing branches to the remote repository.

这会将所有本地存在的分支推送到远程存储库。

To get to your next question, you should be able to do the same thing by using a different set of commands. Try these:

要回答下一个问题,您应该能够使用不同的命令集来做同样的事情。试试这些:

$ cd /var/www  # or wherever
$ mkdir somesite
$ cd somesite/
$ git init
$ git --bare update-server-info
$ git config receive.denycurrentbranch ignore
$ cat > hooks/post-receive
#!/bin/sh
git checkout -f
^D
$ chmod +x hooks/post-receive

You would, of course, run the remote/push commands above afterthis step. You may have to check out a specific branch after doing so, so that the "somesite" clone on the server actually knows which branch to follow. From then on out, pushing to that repository should trigger a re-checkout of that branch.

当然,您会这一步之后运行上面的远程/推送命令。这样做之后,您可能必须检查特定的分支,以便服务器上的“somesite”克隆实际上知道要遵循哪个分支。从那时起,推送到该存储库应该会触发该分支的重新检出。

回答by OOPMan

I also ran into this issue recently and solved it as follows:

我最近也遇到了这个问题,解决方法如下:

On remote server:

在远程服务器上:

1: Create a directory named /tmp/bare
2: Change to that directory
3: Execute git init --bare

1:创建一个名为 /tmp/bare 的目录
2:切换到该目录
3:执行 git init --bare

On local machine:

在本地机器上:

1: Change to your git project directory
2: git remote add bare ssh://user@server/tmp/bare
3: git push --all bare
4: git remote remove bare

1: 切换到你的 git 项目目录
2: git remote add bare ssh://user@server/tmp/bare
3: git push --all bare
4: git remote remove bare

On remote server:

在远程服务器上:

1: git clone /tmp/bare /path/to/your/clone

1: git clone /tmp/bare /path/to/your/clone

On local machine:

在本地机器上:

1: git remote add origin ssh://user@server/path/to/your/clone

1: git remote add origin ssh://user@server/path/to/your/clone

This is a little involved, but it works and does not require setting any weird flags or instructing git to override its default behaviours. It is hence quite safe.

这有点复杂,但它有效并且不需要设置任何奇怪的标志或指示 git 覆盖其默认行为。因此是相当安全的。

回答by AmaDaden

This answer is good but I was not able to get it to work for me. The following code from this link did http://thelucid.com/2008/12/02/git-setting-up-a-remote-repository-and-doing-an-initial-push/. On the remote run

这个答案很好,但我无法让它对我来说有效。此链接中的以下代码执行http://thelucid.com/2008/12/02/git-setting-up-a-remote-repository-and-doing-an-initial-push/。在远程运行

mkdir my_project.git
cd my_project.git
git init --bare
git-update-server-info # If planning to serve via HTTP

Locally on an existing repository that already has at least one commit run

本地在已经有至少一次提交运行的现有存储库上

git remote add origin [email protected]:my_project.git
git push -u origin master

I hope this helps anyone that had problems with the other answer.

我希望这可以帮助任何对其他答案有问题的人。

回答by presto8

Easiest git equivalent to hg clone . ssh://account@server/wwwis:

最简单的 git 等效于hg clone . ssh://account@server/www

rsync -avz . ssh://account@server/www/reponame

In fact, I have added this line to ~/.bash_aliases to mirror any directory anywhere:

事实上,我已将此行添加到 ~/.bash_aliases 以在任何位置镜像任何目录:

alias mirror="rsync -avz . ssh://account@server`pwd` --delete"

It could prove dangerous if you happen to be in a special directory like /dev or /bin. Be careful.

如果您碰巧位于 /dev 或 /bin 等特殊目录中,则可能会很危险。当心。

回答by jlettvin

I agree with, and improve on presto8 by deleting unmatched files.

我同意并通过删除不匹配的文件来改进 presto8。

rsync -avz . ssh://account@server/www/reponame --delete

回答by dani24

Just to give you an alternative, you can use:

只是为了给您一个替代方案,您可以使用:

git remote set-url origin git://other.url.here

These also work if your local git respository is pointing to another remote repository

如果您的本地 git 存储库指向另一个远程存储库,这些也适用