Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting up post-receive hook for bare repo

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.

like image 693
david.colais Avatar asked Jan 22 '13 07:01

david.colais


People also ask

How do I setup a git hook?

To install the hook, you can either create a symlink to it in . git/hooks , or you can simply copy and paste it into the . git/hooks directory whenever the hook is updated. As an alternative, Git also provides a Template Directory mechanism that makes it easier to install hooks automatically.

How do you add pre-commit hooks to repo?

If you want to manually run all pre-commit hooks on a repository, run pre-commit run --all-files . To run individual hooks use pre-commit run <hook_id> . The first time pre-commit runs on a file it will automatically download, install, and run the hook. Note that running a hook for the first time may be slow.


1 Answers

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.

like image 186
VonC Avatar answered Sep 21 '22 13:09

VonC