Git:将现有存储库从电脑移动到服务器,从服务器克隆。

16

我有一个现成的Git代码库在本地电脑上。我想把这个代码库移动到我的Web服务器上,然后通过git clone从服务器中检出代码库来进行开发。我计划在本地电脑上进行开发并将更新推送回服务器。我可以从本地电脑通过SSH连接到服务器,但反之则不行。我应该如何操作呢?我认为应该使用git bundle,但是当我尝试在服务器上git clone我的捆绑包时,出现了“警告:远程HEAD引用不存在于参考中,无法检出”的错误。我的本地电脑正在运行OS X,服务器正在运行Linux。

3个回答

29

在Linux服务器上,进入一个新的目录并执行以下命令:

git init --shared --bare

然后在你的本地机器上:

git remote add origin server:path/to/repo
git push --all origin

完成后,服务器将拥有整个代码库的完整副本,您可以使用push和pull从本地与服务器同步。当您已经有了本地副本时,无需再从服务器上检出另一个克隆版本。


执行 git push origin master 后,我得到了一个以 "Total 0 (delta 0), reused 0 (delta 0)" 开头的长错误信息。远程服务器返回了以下错误:"remote: error: refusing to update checked out branch: refs/heads/master" 和 "remote: error: By default, updating the current branch in a non-bare repository"。在 Linux 服务器上,我的仓库只包含 ". .. .git HEAD branches config description hooks info objects refs" 这些内容,这是使用 git init --shared --bare 命令创建的。我在两台计算机上都使用 git 1.7.1.1 版本。 - Sarah Vessels
哇,不用管了!我刚刚让它工作了。在运行 git init... 命令之前,我实际上没有在 Linux 服务器上创建一个新目录,我只是删除了我认为的所有内容。创建一个新目录,进行 git init...,切换到使用本地 repos 的源,然后推送就可以了。 - Sarah Vessels
原则上来说这个方法是可行的,但我需要额外进行几个步骤以使其生效,因为我的服务器使用了非标准的ssh端口:在OS X上。我首先安装了ssh-askpass(https://github.com/markcarver/mac-ssh-askpass),然后我还需要在~/.ssh/config中配置端口(参见https://dev59.com/JHI_5IYBdhLWcg3wBuX3#5738592),使用SCP样式的URL。 - xgretsch

9

不要使用"git push origin master",而是使用"git push --all origin",这样你就可以移动所有分支,而不仅仅是主分支。


2
这个怎么样:
local> cd my_repo.git
local> git remote add origin user@host:/path/to/my_repo.git
local> git config branch.master.remote origin
local> git config branch.master.merge refs/heads/master
local> git push origin master

这将把本地仓库中的数据发送到您的服务器。然后执行以下操作:

local> cd ..
local> git clone user@host:/path/to/my_repo.git my_repo2.git

那么您将从服务器克隆。当满意时,您可以摆脱原始存储库,并可能重命名第二个存储库。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接