git post-receive checkout to remote machine?

If you add the following script as a hooks/post-receive hook to a bare git repository foo.git:

GIT_WORK_TREE=/bar git checkout -f

then whenever someone pushes to the repository the current state will be updated in directory bar.

  • Use Git / Github to sync my Projects between PC / Laptop / WebInterface
  • Filter lines by pattern in bash script
  • Shell script helper for git commits
  • How can you access an environment variable that has a space in its name in bash?
  • Git bash can't access home directory on fresh machine
  • Git Bash giving sh.exe: ruby: command not found
  • This requires bar and foo.git to be on the same machine.

    What’s the easiest way to modify it so that the checkout is made on a remote machine (say baz:/bar) ?

    One way would be to:

    GIT_WORK_TREE=/tmp/bar git checkout -f
    rsync ... /tmp/bar baz:/bar

    Is there a better way? Perhaps not requiring an intermediate temp directory? (If not what are the correct options to pass to rsync such that the resulting directory is indentical to being checked out directly?)

  • Why does Git automatically ignore most Node files?
  • HOW TO Avoid git diff putting code block name in hunk header?
  • How to get a particular branch from github
  • Bash Script to Push/Pull from Private Git Repository?
  • Merge conflicts ruin my commit message while squashing commits
  • How to prevent the OpenSSH warning about host key offence?
  • One Solution collect form web for “git post-receive checkout to remote machine?”

    GIT_WORK_TREE=/bar means that bar is a git repo.

    If bar is a git repo on the remote side, then it can pull from a bare repo bare_bar.git (also on the remote side), to which you can push to.

    In other words, your post-receive hook would push to bare_bar repo through ssh, and a post-receive hook on that bare repo would trigger the pull from the actual repo bar: see “Creating a git repository from a production folder”.

    I’d prefer a solution that keeps the remote server free of any .git dirs (and even the git package itself ideally)

    In that case, your current post-receive hook and its rsync command seems to be the only way to incrementally copy new data to a remote working tree.

    • git archive would archive everything each time.
    • git bundle would require git on the other side.
    Git Baby is a git and github fan, let's start git clone.