2011-08-22 46 views
9

我需要将git推送到远程,并且该推送需要是当前的工作副本。因为,推送源是一个Web主机。所以,推送的文件需要被当前使用。我相信没有人会在主机:)git force push current工作目录

这个问题是跟进的这2个问题, Is it possible to have a git repo inside another git repowhat difference does --bare switch make when initing a git repo?

编辑编辑文件:裸露的回购协议是不是我想要的,因为文件需要直接在远程机器上使用,需要在我放置它们的位置找到。

EDIT2:这是我的理解是裸露的回购不保留文件hieararchy,并保留回购的数据在根目录

回答

10

这里是如何在网络上创建一个git回购一步一步主机将允许直接推送,将其设置为本地回购的远程服务器,并推送更改。请注意,您需要ssh访问您的虚拟主机,并安装了git。

在您的虚拟主机(以SSH方式登录):

# create an empty repo on your web host; note its path 
mkdir site 
cd site 
git init 

# configure it to allow pushes to the current checked-out branch 
# without complaining (direct push) 
git config receive.denyCurrentBranch ignore 
与您当地的回购(运行回购这些里面Git命令)

现在:

# create the reference to the web host remote for pushing 
# /path/to/the/repo is the path to the repo you made above on the web host: 
# it will be an absolute path OR relative to your ssh chroot (depends on the host) 
git remote add deploy ssh://[email protected]_host/path/to/the/repo 

# finally, push your changes! 
git push deploy master 

现在,你的变化是推送到Web主机上的签出回购。但是,它们不会反映在工作目录中(也就是说,您的更改不会立即生效)。这是因为你已经直接推送到活动分支。要完成了,你需要或者

  • 手动执行git checkout -f更新工作目录
  • 做出git post-receive钩来自动执行。

要使用post-receive挂钩进行即时自动部署,请检查此git site deployment howto。

+0

嗯,我所需要的一切都是recieve.denyCurrentBranch。谢谢:) – yasar

+0

@ yasar11732,请务必阅读有关保持工作目录最新的注意事项。没有它,你推动的变化将反映在Git的历史记录中,但不一定是你的网站的检出分支。 – shelhamer

+0

phew,这对我来说是一项艰巨的工作,但仍然将其取消。谢谢! – yasar

4

虽然这是相当古老,它是非常灰心,推到一个工作树。 https://git.wiki.kernel.org/index.php/GitFaq#Unexpected_behavior

更好的方法是创建一个裸露的回购镜像网站的工作树。

所有裸回购是,是没有工作树或没有检出文件的存储库。层次结构存在所有考虑事项并可以检出。换句话说,它只能作为主机进行更改。

不知道你与你的网站,我会承担它的工作目录层次是利用chroot的家 一个标准的网站布局EG:/ home/user中/ WWW

使用SSH与服务器安装的git:

创建当前网站的工作树

cd /home/user/public_html 
git init 
git add . 
git commit -m "Initial Commit" 

从本地系统创建一个纯仓库到远程推

mkdir /home/user/deploy.git 
cd /home/user/deploy.git 
git init --bare 

链接你的工作树库和裸露的部署库

cd /home/user/public_html 
git remote add deploy /home/user/deploy.git 
git remote show deploy 
* remote deploy 
    URL: /home/user/deploy.git 
git push deploy master 

现在成立了一个新的回购您的本地系统上

git clone ssh://[email protected]/home/user/deploy.git 
git branch -a 
*master 
    remotes/origin/HEAD 
    remotes/origin/master 

现在我们设置2钩立即更改您的网络远程回购当你推动它或如果别人你给予访问推动它。由于混帐配置receive.denyCurrentBranch不理会带来麻烦,从长远来看

在远程服务器上启用部署更新后的

cd /home/user/deploy.git/hooks 
mv post-update.sample post-update 
vi post-update 

更改您的post-update挂钩到以下和保存

#!/bin/sh 
echo "Pulling changes into public_html [deploy post-update]" 
cd /home/user/public_html || exit 
unset GIT_DIR 
git pull deploy master 
exec git update-server-info 

现在,我们设置您的Web工作树,以推动其部署的更改,如果有事情承诺它。

cd /home/user/public_html/.git/hooks 
mv post-commit.sample post-commit 
vi post-commit 

然后改变post-commit钩子以下

#!/bin/sh 
echo "Pushing changes to deploy [public_html post-commit]" 
git push deploy 

你还有如果你需要检出您的网络工作树的选项。 当您推送本地系统的主服务器时,这将允许您将更改从部署部署到Web的工作树。 您可以在不影响网络工作树的情况下分支,重新绑定,还原等,而不用担心冲突标记,而只需使用裸露的部署存储库。 如果您需要更多控制承诺的内容,则可以使用post-receive而不是更新后的版本。

希望这可以帮助别人寻找和OP一样的东西。

5

这个问题真的很老,但我找到了比这里列出的解决方案更好的解决方案(至少对我来说)。

manualgit config,作为Git的2.3.0:

另一种选择是“updateInstead”将更新工作目录(必须是干净的),如果推到当前分支。此选项用于同步工作目录,当通过交互式ssh无法轻松访问一个工作目录时(例如,实时网站,因此要求工作目录清洁)。

因此git config receive.denyCurrentBranch updateInstead(在接收存储库上)为我完美工作。它允许我推动自动更新接收储存库的工作目录(只要该推送发生时该工作目录是干净的)。