Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
358 views
in Technique[技术] by (71.8m points)

githooks - How to set up a Git hook so that after pushing to ssh://peter@foo.com/~/bar.com.git, it will go to ~/bar.com and do a git pull?

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.git on the remote machine (the full path is ssh://peter@www.foo.com/~/bar.com.git

How can a hook be added, so that after the push, the remote server will cd ~/bar.com and do a git pull so that all content is updated (the same as the local machine)? (no need to run git update like for 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.com and cd ~/bar.com and wait there and do a git pull whenever after a git push from the local machine, but it'd be nice to have it done automatically)

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.

Update 2: I went to ~/bar.com.git/hooks and add a new file post-receive with the content:

#!/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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can add a post-receive hook to the ~/bar.com.git repo for this. To do this add to the ~/bar.com.git/hooks/ directory an executable file post-receive with the content:

#!/bin/sh

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

Make sure the post-receive file has the executable bits(e.g. 755).

Now whenever something is pushed to the ~/bar.com.git repo, the ~/bar.com repo is updated automatically.

See also

to understand why unsetting some environment variables is necessary.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...