昨天,我发布了一个关于如何从我的一台机器克隆Git存储库到另一台机器的问题,我如何从另一台机器“克隆Git”?
现在我可以成功地将Git存储库从源存储库(192.168.1.2)克隆到目标存储库(192.168.1.1)。
但是当我对文件进行编辑,git commit -a -m "test"和git push时,我在目的地(192.168.1.1)上得到了这个错误:
git push
hap@192.168.1.2's password:
Counting objects: 21, done.
Compressing objects: 100% (11/11), done.
Writing objects: 100% (11/11), 1010 bytes, done.
Total 11 (delta 9), reused 0 (delta 0)
error: refusing to update checked out branch: refs/heads/master
error: By default, updating the current branch in a non-bare repository
error: is denied, because it will make the index and work tree inconsistent
error: with what you pushed, and will require 'git reset --hard' to match
error: the work tree to HEAD.
error:
error: You can set 'receive.denyCurrentBranch' configuration variable to
error: 'ignore' or 'warn' in the remote repository to allow pushing into
error: its current branch; however, this is not recommended unless you
error: arranged to update its work tree to match what you pushed in some
error: other way.
error:
error: To squelch this message and still keep the default behaviour, set
error: 'receive.denyCurrentBranch' configuration variable to 'refuse'.
To git+ssh://hap@192.168.1.2/media/LINUXDATA/working
! [remote rejected] master -> master (branch is currently checked out)
error: failed to push some refs to 'git+ssh://hap@192.168.1.2/media/LINUXDATA/working'
我正在使用两个不同版本的Git(远程上的1.7版本和本地机器上的1.5版本)。这是一个可能的原因吗?
例如,一旦创建了空(裸)存储库,就需要更改远程服务器上的配置文件
root@development:/home/git/repository/my-project# cat config
你会看到
[core]
repositoryformatversion = 0
filemode = true
bare = false
logallrefupdates = true
你将使这裸露为假为真,我删除了logallrefupdates = true(不确定它的使用!)
to
[core]
repositoryformatversion = 0
filemode = true
bare = true
您可以测试以下内容
$ git remote show origin
* remote origin
Fetch URL: my-portal@development:/home/XYZ/repository/XYZ
Push URL: my-portal@development:/home/XYZ/repository/XYZ
HEAD branch: (unknown)
这个HEAD分支:(未知)将显示如果你不能PUSH。因此,如果HEAD分支是未知的,你应该将bare更改为true,在推送成功后,你可以重用
git remote show origin
你们会看到
HEAD branch: master
下面是一个测试,你可以看看裸服务器的东西是如何工作的:
想象一下,您有一个工作站和一个服务器,服务器上托管着活动站点,您希望不时地更新这个站点(这也适用于两个开发人员通过一个中间人来回发送他们的工作的情况)。
初始化
在你的本地计算机上创建一个目录,并将cd放入其中,然后执行以下命令:
# initialization
git init --bare server/.git
git clone server content
git clone server local
首先创建一个裸服务器目录(注意末尾的.git)。此目录将仅作为存储库文件的容器。
然后将服务器存储库克隆到新创建的内容目录。这是您的活动/生产目录,将由服务器软件提供服务。
前两个目录位于您的服务器上,第三个是您工作站上的本地目录。
工作流
下面是基本的工作流程:
进入本地目录,创建一些文件并提交。最后将它们推送到服务器:
#创造疯狂的东西
Git提交-av
Git push origin master
现在进入内容目录并更新服务器的内容:
git拉
重复1 - 2。这里的内容可能是另一个开发人员也可以推送到服务器上的,并且您可以从他那里获取本地内容。
只需几个设置步骤,您就可以轻松地使用一行程序部署更改到您的网站
git push production
这很简单,你不需要登录到远程服务器并进行拉取或其他操作。请注意,如果您不将生产检出用作工作分支,这将是最好的工作方式!(OP的工作环境略有不同,我认为@Robert Gould的解决方案很好地解决了这个问题。此解决方案更适合部署到远程服务器。)
首先,你需要在你的服务器上的某个地方建立一个裸库,在你的webroot之外。
mkdir mywebsite.git
cd mywebsite.git
git init --bare
然后创建文件hooks/post-receive:
#!/bin/sh
GIT_WORK_TREE=/path/to/webroot/of/mywebsite git checkout -f
并使文件可执行:
chmod +x hooks/post-receive
在本地机器上,
git remote add production git@myserver.com:mywebsite.git
git push production +master:refs/heads/master
都准备好了!现在,将来您可以使用git push production来部署您的更改!
这个解决方案的功劳归http://sebduggan.com/blog/deploy-your-website-changes-using-git/。在那里可以找到关于正在发生的事情的更详细的解释。