Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set up a Git hook so that after pushing to ssh://[email protected]/~/bar.com.git, it will go to ~/bar.com and do a git pull?

Tags:

git

githooks

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

like image 578
nonopolarity Avatar asked Apr 24 '11 08:04

nonopolarity


People also ask

How do I add 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.

Can I push Git hooks to remote?

No. Hooks are per-repository and are never pushed.

How do you push a pre-commit hook?

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.

What is Git pre hook?

The pre-commit hook is run first, before you even type in a commit message. It's used to inspect the snapshot that's about to be committed, to see if you've forgotten something, to make sure tests run, or to examine whatever you need to inspect in the code.


2 Answers

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

  • getting "fatal: not a git repository: '.'" when using post-update hook to execute 'git pull' on another repo
  • Git - post-receive hook with git pull “Failed to find a valid git directory”

to understand why unsetting some environment variables is necessary.

like image 54
Frank S. Thomas Avatar answered Sep 26 '22 11:09

Frank S. Thomas


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 
like image 31
wolf Avatar answered Sep 23 '22 11:09

wolf