如何设置 Git 钩子,以便在推送到 ssh://[email protected]/~/bar.com.git 后,它会转到 ~/bar.com 并执行 git pull?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5769568/
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
How to set up a Git hook so that after pushing to ssh://[email protected]/~/bar.com.git, it will go to ~/bar.com and do a git pull?
提问by nonopolarity
I was advised to set up on a remote server
我被建议在远程服务器上设置
foo.com/~/bar.com # live webpage content
foo.com/~/bar.com.git # a bare repo
so, from my local machine, I can do a
所以,从我的本地机器,我可以做一个
git push
and it will push to foo.com/~/bar.com.git
on the remote machine (the full path is ssh://[email protected]/~/bar.com.git
它将推送到foo.com/~/bar.com.git
远程机器上(完整路径是ssh://[email protected]/~/bar.com.git
How can a hookbe added, so that after the push, the remote server will cd ~/bar.com
and do a git pull
so that all content is updated (the same as the local machine)? (no need to run git update
like for Mercurial?)
如何添加钩子,以便在推送后,远程服务器将cd ~/bar.com
并执行agit pull
以便更新所有内容(与本地机器相同)?(不需要git update
像 Mercurial 那样运行?)
(this is related to Cannot git clone a folder on a server and then edit and git push?right now I can ssh to foo.com
and cd ~/bar.com
and wait there and do a git pull
whenever after a git push
from the local machine, but it'd be nice to have it done automatically)
(这与Cannot git clone a folder on a server and then edit and git push有关?现在我可以ssh到foo.com
并cd ~/bar.com
在那里等待并git pull
在git push
从本地机器之后做一个,但它会很好自动完成)
Update: please only post an answer if you know specific details and how to do it. If you google and post the first or second google result here, it is not going to help.
更新:如果您知道具体的细节以及如何去做,请只发布答案。如果您在 google 上搜索并在此处发布第一个或第二个 google 结果,则无济于事。
Update 2: I went to ~/bar.com.git/hooks
and add a new file post-receive
with the content:
更新 2:我去~/bar.com.git/hooks
添加一个post-receive
包含内容的新文件:
#!/bin/bash
cd ~/bar.com
git pull ../bar.com.git master
and also chmod 755 post-receive
, and if I edit a file on the local machine, and then git com -m "ok"
and git push
, it doesn't make the change go into the remote machine's folder ~/bar.com
而且chmod 755 post-receive
,如果我在本地机器上编辑一个文件,然后git com -m "ok"
和git push
,它不会使更改进入远程机器的文件夹~/bar.com
回答by Frank S. Thomas
You can add a post-receive
hook to the ~/bar.com.git
repo for this. To do this add to the ~/bar.com.git/hooks/
directory an executable file post-receive
with the content:
您可以post-receive
为此向~/bar.com.git
repo添加一个钩子。为此,向~/bar.com.git/hooks/
目录中添加一个post-receive
包含以下内容的可执行文件:
#!/bin/sh
unset $(git rev-parse --local-env-vars)
cd ~/bar.com
git pull
Make surethe post-receive
file has the executable bits(e.g. 755).
请确保该post-receive
文件具有可执行位(例如755)。
Now whenever something is pushed to the ~/bar.com.git
repo, the ~/bar.com
repo is updated automatically.
现在每当有东西被推送到~/bar.com.git
repo 时,~/bar.com
repo 会自动更新。
See also
也可以看看
- getting "fatal: not a git repository: '.'" when using post-update hook to execute 'git pull' on another repo
- Git - post-receive hook with git pull “Failed to find a valid git directory”
- 使用 post-update hook 在另一个 repo 上执行 'git pull' 时得到“fatal: not a git repository: '.'”
- Git - 带有 git pull 的 post-receive 钩子“找不到有效的 git 目录”
to understand why unsetting some environment variables is necessary.
了解为什么需要取消设置某些环境变量。
回答by wolf
I use the following post-update
script (make sure you set executable bit on it):
我使用以下post-update
脚本(确保在其上设置了可执行位):
#!/bin/sh
rm -rf ~/public_html/xxx
git-archive --format=tar --prefix=xxx master | tar x -C ~/public_html
It does the thing, but there's a short window when the site is not available. Doing git pull
directly in yours ~/bar.com
will expose git's .git
directory, so make sure you either block it in your http server, or keep .git
somewhere higher in directory hierarchy, ie. something like:
它可以做到这一点,但是当站点不可用时,会有一个很短的窗口。git pull
直接在你~/bar.com
的.git
目录中做会暴露 git 的目录,所以确保你要么在你的 http 服务器中阻止它,要么.git
在目录层次结构中保持更高的位置,即。就像是:
~
\
- repo
\
- .git
- bar.com
\
- your content