是否可以使用git push部署一个网站?我有一种预感,它与使用git挂钩来执行git重置有关——在服务器端很难,但我该如何完成这一点呢?
当前回答
听起来你应该在你的服务器上有两个副本。一个裸拷贝,你可以推/拉,当你完成时,你会推你的改变,然后你会把这个克隆到你的web目录,并设置一个cronjob来每天从你的web目录更新git pull。
其他回答
我的方法是在我的部署服务器上有一个裸露的Git存储库,我在那里推送更改。然后我登录到部署服务器,更改到实际的web服务器文档目录,并执行git拉取。我没有使用任何钩子来自动地做这个,这似乎比它的价值更麻烦。
我在这个网站上找到了这个脚本,它似乎工作得很好。
复制你的。git目录到你的web服务器 在你的本地副本上,修改你的.git/config文件,并添加你的web服务器作为远程服务器: (远程“生产”) Url = username@webserver:/path/to/htdocs/.git 在服务器上,用这个文件替换。git/hooks/post-update(在下面的答案中) 添加对文件的执行访问(同样是在服务器上): Chmod +x .git/hooks/post-update 现在,只要本地推送到你的web服务器,它就会自动更新工作副本: Git推送生产
使用下面的更新后文件:
复制你的。git目录到你的web服务器 在你的本地副本上,修改你的.git/config文件,并添加你的web服务器作为远程服务器: (远程“生产”) Url = username@webserver:/path/to/htdocs/.git 在服务器上,用下面的文件替换。git/hooks/post-update 添加对文件的执行访问(同样是在服务器上): Chmod +x .git/hooks/post-update 现在,只要本地推送到你的web服务器,它就会自动更新工作副本: Git推送生产
#!/bin/sh
#
# This hook does two things:
#
# 1. update the "info" files that allow the list of references to be
# queries over dumb transports such as http
#
# 2. if this repository looks like it is a non-bare repository, and
# the checked-out branch is pushed to, then update the working copy.
# This makes "push" function somewhat similarly to darcs and bzr.
#
# To enable this hook, make this file executable by "chmod +x post-update".
git-update-server-info
is_bare=$(git-config --get --bool core.bare)
if [ -z "$is_bare" ]
then
# for compatibility's sake, guess
git_dir_full=$(cd $GIT_DIR; pwd)
case $git_dir_full in */.git) is_bare=false;; *) is_bare=true;; esac
fi
update_wc() {
ref=$1
echo "Push to checked out branch $ref" >&2
if [ ! -f $GIT_DIR/logs/HEAD ]
then
echo "E:push to non-bare repository requires a HEAD reflog" >&2
exit 1
fi
if (cd $GIT_WORK_TREE; git-diff-files -q --exit-code >/dev/null)
then
wc_dirty=0
else
echo "W:unstaged changes found in working copy" >&2
wc_dirty=1
desc="working copy"
fi
if git diff-index --cached HEAD@{1} >/dev/null
then
index_dirty=0
else
echo "W:uncommitted, staged changes found" >&2
index_dirty=1
if [ -n "$desc" ]
then
desc="$desc and index"
else
desc="index"
fi
fi
if [ "$wc_dirty" -ne 0 -o "$index_dirty" -ne 0 ]
then
new=$(git rev-parse HEAD)
echo "W:stashing dirty $desc - see git-stash(1)" >&2
( trap 'echo trapped $$; git symbolic-ref HEAD "'"$ref"'"' 2 3 13 15 ERR EXIT
git-update-ref --no-deref HEAD HEAD@{1}
cd $GIT_WORK_TREE
git stash save "dirty $desc before update to $new";
git-symbolic-ref HEAD "$ref"
)
fi
# eye candy - show the WC updates :)
echo "Updating working copy" >&2
(cd $GIT_WORK_TREE
git-diff-index -R --name-status HEAD >&2
git-reset --hard HEAD)
}
if [ "$is_bare" = "false" ]
then
active_branch=`git-symbolic-ref HEAD`
export GIT_DIR=$(cd $GIT_DIR; pwd)
GIT_WORK_TREE=${GIT_WORK_TREE-..}
for ref
do
if [ "$ref" = "$active_branch" ]
then
update_wc $ref
fi
done
fi
Giddyup是语言无关的只是添加水的git挂钩,通过git推送自动化部署。它还允许你有自定义启动/停止钩子重新启动web服务器,加热缓存等。
https://github.com/mpalmer/giddyup
请看一些例子。
听起来你应该在你的服务器上有两个副本。一个裸拷贝,你可以推/拉,当你完成时,你会推你的改变,然后你会把这个克隆到你的web目录,并设置一个cronjob来每天从你的web目录更新git pull。
推荐文章
- 结合Git存储库的前两次提交?
- Xcode 6 gitignore文件应该包括什么?
- 我如何从现有回购的分支创建一个新的GitHub回购?
- 如何将Git分支移到它自己的存储库中?
- 如何禁用Git凭证管理器的Windows?
- Git 拉取与 Git 变基
- 在git中压缩提交是什么意思?
- 在GIT中合并2个分支
- git标签也会被推送吗?
- 嵌套的Git存储库?
- Git bash错误:无法fork子进程:没有可用的终端(-1)
- 有没有办法在SourceTree中获得两个分支的视觉差异?
- Git标记是否只应用于当前分支?
- Git分支:master vs. origin/master vs. remotes/origin/master
- 如何根据文件扩展名过滤git差异?