在 git 挂钩中自动执行 post-commit 以同步本地和远程目录时出现问题
Issue in automatic executing post-commit in git hook to sync local and remote directory
我有一个只有 git 存储库的服务器,我想使用挂钩将内容与工作目录同步,这样我就可以将我的本地目录同步到服务器中的工作目录。
到处谷歌搜索;这是我到目前为止所做的。
ssh 设置
来自 and Dummy questions about setting up git on amazon cloud ec2
本地:ssh-keygen -t rsa -b 1024
并将其命名为 id_rsa_aws
。然后创建 id_rsa_asw
和 id_rsa_aws.pub
文件。
访问服务器:ssh -i amazon-generated-key.pem ubuntu@example.com
将生成的 id_rsa_aws.pub
密钥附加到 ~/.ssh/authorized_keys
。
正在创建一个 git 裸存储库
服务器:mkdir -p ~/git/dumb.git; cd ~/git/dumb.git; git init --bare
本地:git clone ubuntu@example.com:git/dumb.git
- What does git "updating currently checked out branch" warning mean?
从服务器的工作目录推送
服务器:git clone ~/git/dumb.git
从本地工作目录推送。
在本地目录中进行一些更改。
- 本地:
git add .; git commit -m "...";
- 本地:
git push --set-upstream origin master
- 这是一次性执行,那我可以用
git push
- Why can't I push to this bare repository?
创建挂钩
创建 ~/git/dumb.git/hooks/post-commit
,然后使用 chmod a+x post-commit
使其可运行。
#!/bin/bash
unset GIT_INDEX_FILE
git --work-tree=/home/ubuntu/dumb --git-dir=/home/ubuntu/git/dumb.git checkout -f
问题
当我从本地机器推送到服务器时,没有问题。
dumb> git push
Counting objects: 3, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 278 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To ubuntu@prosseek.com:git/dumb.git
5964789..b29c160 master -> master
但是,post-commit 不会自动调用。我可以登录到服务器,并执行 post-commit
并同步工作目录。
可能出了什么问题?
钩子错了,钩子脚本应该是post-receive
而不是post-commit
。提示来自
https://www.digitalocean.com/community/tutorials/how-to-use-git-hooks-to-automate-development-and-deployment-tasks
这是post-receive
挂钩的内容。
#!/bin/bash
unset GIT_INDEX_FILE
while read oldrev newrev ref
do
if [[ $ref =~ .*/master$ ]];
then
echo "Master ref received. Deploying master branch to production..."
git --work-tree=/home/ubuntu/dumb --git-dir=/home/ubuntu/git/dumb.git checkout -f master
echo "Ref $ref successfully received. Doing nothing: only the master branch may be deployed on this server."
fi
done
我有一个只有 git 存储库的服务器,我想使用挂钩将内容与工作目录同步,这样我就可以将我的本地目录同步到服务器中的工作目录。
到处谷歌搜索;这是我到目前为止所做的。
ssh 设置
来自 and Dummy questions about setting up git on amazon cloud ec2
本地:ssh-keygen -t rsa -b 1024
并将其命名为 id_rsa_aws
。然后创建 id_rsa_asw
和 id_rsa_aws.pub
文件。
访问服务器:ssh -i amazon-generated-key.pem ubuntu@example.com
将生成的 id_rsa_aws.pub
密钥附加到 ~/.ssh/authorized_keys
。
正在创建一个 git 裸存储库
服务器:mkdir -p ~/git/dumb.git; cd ~/git/dumb.git; git init --bare
本地:git clone ubuntu@example.com:git/dumb.git
- What does git "updating currently checked out branch" warning mean?
从服务器的工作目录推送
服务器:git clone ~/git/dumb.git
从本地工作目录推送。
在本地目录中进行一些更改。
- 本地:
git add .; git commit -m "...";
- 本地:
git push --set-upstream origin master
- 这是一次性执行,那我可以用
git push
- Why can't I push to this bare repository?
- 这是一次性执行,那我可以用
创建挂钩
创建 ~/git/dumb.git/hooks/post-commit
,然后使用 chmod a+x post-commit
使其可运行。
#!/bin/bash
unset GIT_INDEX_FILE
git --work-tree=/home/ubuntu/dumb --git-dir=/home/ubuntu/git/dumb.git checkout -f
问题
当我从本地机器推送到服务器时,没有问题。
dumb> git push
Counting objects: 3, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 278 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To ubuntu@prosseek.com:git/dumb.git
5964789..b29c160 master -> master
但是,post-commit 不会自动调用。我可以登录到服务器,并执行 post-commit
并同步工作目录。
可能出了什么问题?
钩子错了,钩子脚本应该是post-receive
而不是post-commit
。提示来自
https://www.digitalocean.com/community/tutorials/how-to-use-git-hooks-to-automate-development-and-deployment-tasks
这是post-receive
挂钩的内容。
#!/bin/bash
unset GIT_INDEX_FILE
while read oldrev newrev ref
do
if [[ $ref =~ .*/master$ ]];
then
echo "Master ref received. Deploying master branch to production..."
git --work-tree=/home/ubuntu/dumb --git-dir=/home/ubuntu/git/dumb.git checkout -f master
echo "Ref $ref successfully received. Doing nothing: only the master branch may be deployed on this server."
fi
done