如何设置 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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 10:44:34  来源:igfitidea点击:

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?

gitgithooks

提问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.giton 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.comand do a git pullso that all content is updated (the same as the local machine)? (no need to run git updatelike 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.comand cd ~/bar.comand wait there and do a git pullwhenever after a git pushfrom 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.comcd ~/bar.com在那里等待并git pullgit 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/hooksand add a new file post-receivewith 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-receivehook to the ~/bar.com.gitrepo for this. To do this add to the ~/bar.com.git/hooks/directory an executable file post-receivewith the content:

您可以post-receive为此向~/bar.com.gitrepo添加一个钩子。为此,向~/bar.com.git/hooks/目录中添加一个post-receive包含以下内容的可执行文件:

#!/bin/sh

unset $(git rev-parse --local-env-vars)
cd ~/bar.com
git pull

Make surethe post-receivefile has the executable bits(e.g. 755).

请确保post-receive文件具有可执行位(例如755)。

Now whenever something is pushed to the ~/bar.com.gitrepo, the ~/bar.comrepo is updated automatically.

现在每当有东西被推送到~/bar.com.gitrepo 时,~/bar.comrepo 会自动更新。

See also

也可以看看

to understand why unsetting some environment variables is necessary.

了解为什么需要取消设置某些环境变量。

回答by wolf

I use the following post-updatescript (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 pulldirectly in yours ~/bar.comwill expose git's .gitdirectory, so make sure you either block it in your http server, or keep .gitsomewhere higher in directory hierarchy, ie. something like:

它可以做到这一点,但是当站点不可用时,会有一个很短的窗口。git pull直接在你~/bar.com.git目录中做会暴露 git 的目录,所以确保你要么在你的 http 服务器中阻止它,要么.git在目录层次结构中保持更高的位置,即。就像是:

~
 \
  - repo
    \
     - .git
     - bar.com
       \
        - your content