简体   繁体   English

在主分支更改时自动更新本地 github 存储库

[英]Automatically update local github repository on master branch change

I have a github repository that exists on several different hosts.我有一个存在于多个不同主机上的 github 存储库。 I'm trying to figure out if there's some way to have two of the hosts automatically pull down any new changes to the master branch, without having to log in and issue the pull commands themselves, at the same time I still need to be able to push changes from those hosts on rare occasions.我试图弄清楚是否有某种方法可以让两个主机自动将任何新更改拉到 master 分支,而不必自己登录并发出pull命令,同时我仍然需要能够在极少数情况下从这些主机推送更改。

From what I've seen I believe I should be looking at something with Hooks, but having a hard time understanding how they work.从我所看到的情况来看,我相信我应该用 Hooks 来研究一些东西,但很难理解它们是如何工作的。 It seems like a server side post-receive may be what I'm looking for, but not sure.似乎服务器端post-receive可能是我正在寻找的,但不确定。

Does anyone have any experience with getting this to work?有没有人有让它工作的经验?

Git hooks will allow you to do extra things after issuing the git pull command, but not to periodically run a git pull . Git 挂钩允许您在发出git pull命令后做额外的事情,但不能定期运行git pull I recommend using something outside of git itself for this regular pulling.我建议使用 git 本身之外的东西来进行这种常规拉动。 A cron entry to do this would look something like this:执行此操作的 cron 条目看起来像这样:

* * * * * cd /my/directory/path && git pull >> /var/log/gitcron.log 2>&1  # Pull gitrepo every minute, logging output to /var/log/gitcron.log

Keep in mind, if you have a regular pull set up like this, a rebase or something else that rewrites history in your target branch will cause the machine with the cron to be in a broken state without manual intervention.请记住,如果您有像这样的常规拉动设置,则在目标分支中重写历史记录的变基或其他东西将导致带有 cron 的机器在没有人工干预的情况下处于损坏状态 state。

I had the same issue and created a python script with simple http.server routine.我遇到了同样的问题,并使用简单的 http.server 例程创建了一个 python 脚本。 Here the repository (only built-in libraries): https://github.com/TinKurbatoff/github-webhook-autopull这里的存储库(仅内置库): https://github.com/TinKurbatoff/github-webhook-autopull

Here you may setup a webhook: https://github.com/<github_user_name>/<your_repo>/settings/hooks在这里你可以设置一个 webhook: https://github.com/<github_user_name>/<your_repo>/settings/hooks

and it will call your web server each time this repo is updated.每次更新此 repo 时,它都会调用您的 web 服务器。

Even if you keep your repo copy locally — you may set this listener up at your home/work computer on some port and call your computer via IP (you may need to set up a port forwarding at the home router).即使你在本地保留你的 repo 副本——你可以在你的家庭/工作计算机上的某个端口上设置这个监听器,并通过 IP 调用你的计算机(你可能需要在家庭路由器上设置端口转发)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM