2

Is there a way to have two machines, let's say A and B.

  • A is my local machine, where I develop my code, and push it.
  • B is a server, that will NEVER have to edit code, but NEEDS a working directory.

Is it possible to do this without doing any git pull's whatsoever from B?


I want to use git one way only, basically. In one direction. Sort of like using rsync to sync a local directory to a remote one, but with version control. If this isn't possible with git, is it possible with anything else?

Again, preferably with version control.

question
  • 361
  • 1
  • 8
  • You'll probably want to have B in a detached head state, so that you can push new code to B. Then with a post-receive hook, have B checkout the latest commit directly (not a branch, because you can't update a branch while it's currently checked-out without using `pull` or `merge`). –  Aug 20 '13 at 05:44
  • This may be of interest: [Git - simplest way to sync a repository with a checked out branch](http://stackoverflow.com/questions/4011089/git-simplest-way-to-sync-a-repository-with-a-checked-out-branch) – detly Aug 20 '13 at 06:21

1 Answers1

3

This article explains it very well. I'll copy the code from there.

It assumes you have SSH authentication set up (no password) between your server and workstation.

On the server:

mkdir website.git && cd website.git
git init --bare
mkdir /var/www/www.example.org
touch hooks/post-receive
chmod +x hooks/post-receive

Then put this in hooks/post-receive

#!/bin/sh
GIT_WORK_TREE=/var/www/www.example.org git checkout -f

In your local git repository:

git remote add web ssh://server.example.org/home/ams/website.git
git push web +master:refs/heads/master

Configuration:

  • /var/www/www.example.org is the public root of this site.
  • /home/ams is the location you created website.git
  • server.example.org is the domain, IP, or host alias of your server
tacaswell
  • 84,579
  • 22
  • 210
  • 199
Brigand
  • 84,529
  • 20
  • 165
  • 173
  • hi thank you for this but i cant make it running. Got an error -- fatal: '~/website.git' does not appear to be a git repository fatal: Could not read from remote repository. hope you can help me on this. thanks – drexsien Feb 03 '14 at 09:09
  • @andsien, perhaps you don't have SSH set up; or you're mixing up the commands that need to be run on the server, and the ones that need to be run on your computer. – Brigand Feb 04 '14 at 02:21
  • hi thanks for your reply. i already got it, i did not use the public root of my site. – drexsien Feb 04 '14 at 05:53