14

I have a bare repo set up in my ubuntu server.

After I push to my bare git repo to the server:

$ git push origin master

I want the contents of my non bare repo to be updated with the latest push as shown where the non bare repo is my actual work directory named workfiles.

$ cd /central/workfiles
$ git pull
$ exit

I have heard about the post-receive hook but do not know how to set up the same. How can i achieve the same.

david.colais
  • 619
  • 2
  • 7
  • 17

3 Answers3

13

I prefer specifying the working tree and git directory instead of relying on a cd:

/bare/repo.git/hooks/post-receive

#!/bin/sh
GIT_WORK_TREE=/central/workfiles GIT_DIR=/central/workfiles/.git git pull origin master
exit

As commented below by ChrisV, you can also rely one a git checkout instead of a git pull

I believe git checkout -f is safer than git pull, as the merge which is part of the pull has the potential to make things messy if manual fixups should be needed.

But that means /central/workfiles is not a "non-bare" git repo. It is just a folder where you checkout the content of the bare repo /bare/repo.git.
See Brian Thomas's answer for an example of that approach.

That would not fit the OP specification.

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Should I just create the files from the contents you have given.It appears to me that it is in a single line. – david.colais Jan 22 '13 at 07:57
  • @david.colais 3 lines: the bin/sh, one line for the pull, and one line for `exit` (although that `exit` line isn't really needed) – VonC Jan 22 '13 at 07:59
  • @david.colais so again, the pull itself is one line with two environment variables set: `GIT_WORK_TREE=/central/workfiles GIT_DIR=/central/workfiles/.git git pull origin master` – VonC Jan 22 '13 at 08:04
  • 1
    Late addition: I believe `git checkout -f` is safer than `git pull`, as the merge which is part of the pull has the potential to make things messy if manual fixups should be needed... – ChrisV Feb 09 '15 at 15:25
  • @ChrisV indeed. I have included your comment in the answer for more visibility. – VonC Feb 09 '15 at 15:29
  • Does `git checkout -f` pull the latest changes from the remote? – markasoftware Mar 30 '16 at 02:52
  • @Markasoftware You are right, it would *not*. I have amended the answer to make clear that a checkout would only work if the destination is *not* a git repo (just a folder you update) – VonC Mar 30 '16 at 07:08
6

i use the post receive hook like this

#.git/hooks/post-receive

#!/bin/sh
GIT_WORK_TREE=/srv/http/sitedir/ git checkout -f

yes, make sure to make it executable.

blamb
  • 4,220
  • 4
  • 32
  • 50
  • 1
    is that the tactics @VonC ? to add the snippet from someone else after theh answer to get the more recent datea, and all the votes? lol. i gotta figure out how to get more points around here. can you remove the code snippet that duplicated my answer here plesae if possible and just replace that with see `@Brian Thomas` please, so i can get a few points on that? Im trying to keep things fair and earn some rights on overflow, spotted this.... Thanks for any cooperation. Regards,. ..wait, i just thought of something, was that you that gave me the point? if so, disregard, if no please do :-) – blamb Aug 12 '15 at 07:39
4

I'd go with something like

#!/bin/sh

cd /central/workfiles
git pull
exit

Save the above script as post-receive and place it in the hooks/ directory of your bare repo.

Bottom line don't forget to make it executable

chmod +x post-receive
Gabriele Petronella
  • 106,943
  • 21
  • 217
  • 235
  • I created a new file as post-receive but after I push to bare repo no changes get reflected in the non-bare repo which is my work dir – david.colais Jan 22 '13 at 07:55
  • This will work only if GIT_DIR is set to ".git", otherwise you will get a bug – rambi Jun 15 '21 at 10:31