git - 使用 git Hook 的服务器部署不起作用

标签 git ubuntu server vps

关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。












我们不允许在 Stack Overflow 上提出有关专业服务器或网络相关基础设施管理的问题。您可以编辑问题,使其成为 on-topic对于堆栈溢出。


去年关闭。







Improve this question




在本地机器和 ubuntu vps 服务器中使用 windows 10 时,我正在尝试使用 git hooks 从本地到服务器进行自动部署。我将我的 ubuntu 服务器配置如下:

root@server:~# cd /var
root@server:~# mkdir repo && cd repo
root@server:~# mkdir site.git && cd site.git
root@server:~# git init --bare
root@server:~# cd hooks
root@server:~# touch post-receive && nano post-receive 
并修改 post-receive如下
#!/bin/sh
git --work-tree=/var/www/site--git-dir=/var/repo/site.git checkout -f
之后在本地 pc repo 中我执行了以下命令
git remote add production ssh://root@example.com/var/repo/site.git
现在在本地仓库中,我有 3 个分支,例如 master, branch1, branch2 .当我将 repo 推送到服务器时git push production master它将主分支推送到服务器。但是当我推 git push production branch1 ,它不会推送分支 1。在 /var/www/site我找到了主分支。
所以我有两个问题:
  • 如何将其他分支(不是主分支)推送到服务器。
  • 如果我需要从本地机器上删除生产分支并添加新的服务器目标怎么做?

  • 提前致谢!

    最佳答案

    您可以尝试以下方法:

    #!/bin/bash
    TARGET="/var/www/site"
    GIT_DIR="/var/repo/site.git"
    
    while read oldrev newrev ref
    do
            echo "Ref $ref received. Deploying ${newrev} to production..."
            git --work-tree=$TARGET --git-dir=$GIT_DIR checkout -f $newrev
    done
    
    如果没有在 checkout 命令上指定分支或提交,将再次 check out 现有的 head。

    关于git - 使用 git Hook 的服务器部署不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67263915/

    相关文章:

    linux - 每天使用 cron 运行脚本并创建压缩日志文件

    java - Spring应用服务器通过websocket向客户端传输数据

    Git 重新定位 : shows Rebase 2/1 and some commit is not found

    git - 尝试使用 git 检查另一个分支时发生 fatal error

    git - 在git中重命名文件

    git - git rebase --continue 的行为不一致

    c - 我在哪里可以获得 OpenGL 图形头文件?

    linux - 如何找到现在启动的 linux Image 名称

    apache - 使用htaccess在Xampp上出现错误500(密码)

    java - 线程 "main"java.net.BindException : Address already in use 中的异常