如何使用Git将文件推送到远程服务器

时间:2020-10-21 19:08:40

标签: git

我正在尝试将文件推送到远程服务器。 到目前为止,我已经尝试过了。

远程服务器

$ cd /var
$ mkdir repo && cd repo
$ mkdir test.git && cd test.git
$ git init --bare
$ cd hooks
$ touch post-receive
$ sudo chmod +x post-receive

接收后

#!/bin/sh
git --work-tree=/var/www/testsite --git-dir=/var/repo/test.git checkout -f

本地

$ cd /localrepo
$ git remote add test ssh://user@my-site.com/var/repo/test.git
$ git push test main

输出

Enumerating objects: 917, done.
Counting objects: 100% (917/917), done.
Delta compression using up to 12 threads
Compressing objects: 100% (879/879), done.
Writing objects: 100% (917/917), 3.93 MiB | 2.61 MiB/s, done.
Total 917 (delta 518), reused 0 (delta 0)
remote: Resolving deltas: 100% (518/518), done.
To ssh://user@my-site.com/var/repo/test.git
 * [new branch]      main -> main

期望 我期望在推送之后,我应该能够在远程服务器上的/var/www/testsite中看到文件,但是我找不到任何文件。

post-receive似乎不起作用

我实际上是在尝试执行this中的步骤12

1 个答案:

答案 0 :(得分:1)

问题也出在服务器上的活动分支是“主服务器”,而我正在推送到“主服务器”这一事实。 (感谢@kosist指出这一点。)

解决方案

更改服务器上的活动分支

echo ref: refs/heads/main > HEAD

git symbolic-ref HEAD refs/heads/main

或在post-receive钩子中指定分支(我选择的选项)

git --work-tree=/var/www/laravel --git-dir=/var/repo/site.git checkout -f main
相关问题