37

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://[email protected]/~/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

2 Answers 2

55

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.

6
  • 1
    I think I know something like this, but how? what are the details? you may as well say go buy the Git book and the answer is there Commented Apr 24, 2011 at 9:35
  • 2
    post-receive is simply a shell script. You can put anything in there.
    – troelskn
    Commented Apr 24, 2011 at 9:41
  • 1
    @動靜能量 Try it again with the Git's environment variables unset. Commented Apr 24, 2011 at 12:08
  • Thanks you!!! That unset thing there saved my day :-D I can breathe again... Ah... Commented Dec 11, 2012 at 12:38
  • 1
    +1 exactly what I needed, when I needed it, praise to the hivemind :)
    – Joe
    Commented Jul 23, 2014 at 8:23
1

I use the following post-update script (make sure you set executable bit on it):

#!/bin/sh
rm -rf ~/public_html/xxx
git-archive --format=tar --prefix=xxx master | tar x -C ~/public_html

It does the thing, but there's a short window when the site is not available. Doing git pull directly in yours ~/bar.com will expose git's .git directory, so make sure you either block it in your http server, or keep .git somewhere higher in directory hierarchy, ie. something like:

~
 \
  - repo
    \
     - .git
     - bar.com
       \
        - your content
3
  • 3
    you remove the whole directory and re-populate it? What if the directory is 300MB and all you did was a 1 line change? redo the whole 300MB? Commented Apr 24, 2011 at 10:04
  • It works for me (my site is quite small). In your case the git pull approach would surely be better. Also note: there's no sense to have one repo to which you push to and another one right next to it that would pull from the first one. It seems more sensible to just push to one repo and have it update its contents automatically (git used to work that way, but they have changed that. IIRC there's some option to revert back to the behavior you'd desire.).
    – wolf
    Commented Apr 24, 2011 at 10:48
  • You could also checkout the archive into a temp location and rsync it with the "working" directory.
    – Mark
    Commented May 15, 2013 at 13:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.