简单的 git post-commit 钩子将提交的文件复制到某个文件夹
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7633299/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Simple git post-commit hook to copy committed files to a certain folder
提问by viorel
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
例如:我有一个名为 /example.com 的文件夹和一个 git 存储库。我希望当我在存储库中提交 index.html 时,/example.com 中的相应 index.html 文件将使用已提交文件的内容进行更新
回答by Mark Longair
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:
这样做的一个好方法是创建一个post-commit
运行git checkout -f
时将工作树设置为您的 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
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.
但是,请注意这一点 - 这-f
意味着 git 可能会删除或覆盖文件以/example.com/
匹配您最近提交中的树。
(Remember to make the .git/hooks/post-commit
file executable as well.)
(记住也要使.git/hooks/post-commit
文件可执行。)