简单的 git post-commit 钩子将提交的文件复制到某个文件夹

发布于 2024-12-07 21:30:36 字数 304 浏览 0 评论 0原文

我想自动将提交的文件复制到某个文件夹,以便可以在浏览器中查看它们,但我想这样做而不必创建一个镜像主存储库的裸存储库(如图所示 此处),我希望这在提交时发生。

有没有简单的方法来创建一个钩子来读取已提交的文件并在实时网络服务器上复制/更新它们?

例如:我有一个名为 /example.com 的文件夹和一个 git 存储库。我希望当我在存储库中提交index.html时,/example.com中相应的index.html文件将被更新为所提交文件的内容

I would like to automatically copy the committed files to a certain folder so they can be viewed in a browser, but I would like to do this without having to create a bare repository that mirrors the main repository (as shown here) and I would like this to happen on commit.

Is there any simple way to create a hook that reads which files have been committed and copies them/updates them on a live webserver?

For example: I have a folder named /example.com and a git repository. I want that when I commit index.html in the repository, the corresponding index.html file from /example.com to be updated with the contents of the committed file

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

﹉夏雨初晴づ 2024-12-14 21:30:36

执行此操作的一个好方法是创建一个运行 git checkout -f 的 post-commit ,并将工作树设置为 Web 服务器公开的目录,并且git 目录设置为开发存储库中的 .git 目录。例如,您可以创建一个执行以下操作的 .git/hooks/post-commit 文件:

#!/bin/sh
unset GIT_INDEX_FILE
export GIT_WORK_TREE=/example.com/
export GIT_DIR=/home/whoever/development/web-project/.git/
git checkout -f

但是要小心 - -f 意味着 git 可能会删除或覆盖使 /example.com/ 与您最近提交中的树匹配的文件。

(请记住使 .git/hooks/post-commit 文件也可执行。)

A good way of doing this is to create a post-commit that runs git checkout -f with the work tree set to the directory that is exposed by your web server and the git directory set to the .git directory in your development repository. For example, you could create a .git/hooks/post-commit file that did:

#!/bin/sh
unset GIT_INDEX_FILE
export GIT_WORK_TREE=/example.com/
export GIT_DIR=/home/whoever/development/web-project/.git/
git checkout -f

Be careful with this, however - the -f means that git may remove or overwrite files to make /example.com/ match the tree in your most recent commit.

(Remember to make the .git/hooks/post-commit file executable as well.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文