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.

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

7 Comments

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 3 lines: the bin/sh, one line for the pull, and one line for exit (although that exit line isn't really needed)
@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
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 indeed. I have included your comment in the answer for more visibility.
|
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.

1 Comment

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 :-)
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 

2 Comments

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
This will work only if GIT_DIR is set to ".git", otherwise you will get a bug

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.