通过Git部署网站

我在一个Web开发团队工作.我们每个人都在开发服务器上的主目录中设置了一个个人网站.

我们希望将它们转换为git工作回购,这样我们就可以分支,改造,并且通常享受git goodness带来的好处.

在线阅读时,有几个选择:

1)通过post-receive钩子创建一个与www /中的工作仓库同步的裸仓库

2)直接推送到工作回购

选项1的问题是它不能很好地处理分支.当我们推送除master之外的分支时,post-receive hook仍然只同步master,因此我们的更改永远不会出现在开发站点上.

选项2的问题是git不允许推送到签出分支以防止分离的HEAD状态.

在网上阅读时,选项2的“最佳实践”解决方案是这样的:

1)在CLIENT推送到虚拟分支

2)关于SERVER合并虚拟成主… BUZZZZ错误答案.假设最终用户没有对服务器的shell访问权限.

所以我想,“没问题,只需创建一个像这样的帖子接收钩子”:

#!/bin/bash
read oldSHA newSHA branch
git merge $branch

现在这里是奇怪的部分:当收到后接收挂钩时,我得到了

Please, commit your changes or stash them before you can merge.

是的,我已确认master是服务器上的签出分支,并且虚拟分支已正确更新.当我直接在服务器上运行相同的命令(git merge dummy)时,一切正常.

有谁能解释为什么?

提前致谢.

编辑1

以下是git状态的结果,包括合并前和合并后

Counting objects: 5, done.                                                                                                                                                            
Delta compression using up to 8 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 307 bytes, done.
Total 3 (delta 2), reused 0 (delta 0)
remote: **** PRE MERGE ****
remote: # On branch master
remote: # Untracked files:
remote: #   (use "git add <file>..." to include in what will be committed)
remote: #
remote: #       COMMIT_EDITMSG
remote: #       FETCH_HEAD
remote: #       HEAD
remote: #       ORIG_HEAD
remote: #       config
remote: #       description
remote: #       hooks/
remote: #       index
remote: #       info/
remote: #       logs/
remote: #       objects/
remote: #       packed-refs
remote: #       refs/
remote: no changes added to commit (use "git add" and/or "git commit -a")
remote: error: Your local changes to the following files would be overwritten by merge:
remote:         index.htm
remote: Please, commit your changes or stash them before you can merge.
remote: Aborting
remote: **** POST MERGE ****
remote: # On branch master
remote: # Changes not staged for commit:
remote: #   (use "git add <file>..." to update what will be committed)
remote: #   (use "git checkout -- <file>..." to discard changes in working directory)
remote: #
remote: #       modified:   index.htm
remote: #
remote: # Untracked files:
remote: #   (use "git add <file>..." to include in what will be committed)
remote: #
remote: #       COMMIT_EDITMSG
remote: #       FETCH_HEAD
remote: #       HEAD
remote: #       ORIG_HEAD
remote: #       config
remote: #       description
remote: #       hooks/
remote: #       index
remote: #       info/
remote: #       logs/
remote: #       objects/
remote: #       packed-refs
remote: #       refs/
remote: no changes added to commit (use "git add" and/or "git commit -a")

请注意,我可以手动git add.,git commit -m“foo”并重新执行此过程,结果相同

最佳答案 在其中一个“DUH!”片刻,我意识到我可以使用选项1,只是传递分支名称,就像我尝试选项2一样.

所以,我创建了一个/home/username/www.git裸存储库,将其克隆到/ home / username / www并在/home/username/www.git上添加了一个post-receive hook,它看起来像这样:

#!/bin/bash
read oldSHA newSHA branch
git --git-dir="/home/username/www/.git" --work-tree="/home/username/www" pull /home/username/www.git $branch

然后我将我的遥控器更改为ssh://username@example.com/home/username/www.git(裸机,而不是工作机器人)并通过以下方式推送到该遥控器:

git push remoteName branchName

瞧!一切正常.

实际的接收后代码更复杂,因为我对目录名称使用了一些错误陷阱和变量(因此它们可以部署到任何用户),但是你明白了.

(在阅读代码示例时,请注意区分www / .git和www.git.)

点赞