0

I want to synchronize a directory /home/myproject of my distant machine with, as source, the directory D:\myproject\ of my local machine. I would like to use git (to also benefit from commits history, etc.)

I did this on distant machine (creation of a bare repository, see also What is the difference between "git init" and "git init --bare"?):

mkdir /home/myproject.git
cd /home/myproject.git
git init --bare

and this on local machine (with current directory D:\myproject\):

git init
git add main.py   # D:\myproject\main.py exists on local machine
git commit -m "First"
git remote add dest root@203.0.113.0:/home/myproject.git    # via ssh
git push dest master

It works, now distant server's /home/myproject.git is synchronized, but the directory /home/myproject/ (that should contain for example /home/myproject/main.py) still doesn't exist!

So I have to do this on the distant server:

cd /home
git clone myproject.git myproject

and now /home/myproject/main.py exists.

Problem: each time I do git push on local machine, it's distant server's /home/myproject.git which is updated, and not /home/myproject/.

Question: how to configure these repositories such that git push automatically updates all the files in /home/myproject such as /home/myproject/main.py, instead of only /home/myproject.git?

Basj
  • 29,668
  • 65
  • 241
  • 451
  • About bare repositories: https://stackoverflow.com/q/7861184/7976758. Using GIT to deploy: https://stackoverflow.com/q/18804552/7976758. – phd Jun 20 '18 at 20:08
  • Thank you @phd, I already read https://stackoverflow.com/questions/7861184/what-is-the-difference-between-git-init-and-git-init-bare before posting the question, but it did not really help in this precise request, i.e. `git push` updating not only the myproject.git but above all the destination directory. – Basj Jun 20 '18 at 20:15

2 Answers2

0

As explained in this tutorial, here is a working solution:

  1. On the local machine (from working directory D:\myproject\), create the repository:

    git init
    git add main.py
    git commit -m "First"
    git remote add dest root@203.0.113.0:/home/myproject.git
    
  2. On the distant machine, create a "bare repository" + the destination directory:

    mkdir /home/myproject.git && cd /home/myproject.git && git init --bare
    echo -e '#!/bin/sh\nGIT_WORK_TREE=/home/myproject git checkout -f' > hooks/post-receive
    chmod +x hooks/post-receive
    
    mkdir /home/myproject
    
  3. On local machine:

    git push -u dest master        # later we can just do: git push
    

Now /home/myproject is updated on the distant machine!

Basj
  • 29,668
  • 65
  • 241
  • 451
0

Here is an easier solution (not requiring a "bare" repository or "post-receive hook" script):

  1. On the distant machine, create the destination repository and configure it like this:

    mkdir /home/myproject && cd /home/myproject
    git init
    git config receive.denyCurrentBranch updateInstead
    
  2. On the local machine (from working directory D:\myproject\), create the source repository, and push it:

    git init
    git add main.py
    git commit -m "First"
    git remote add dest root@203.0.113.0:/home/myproject
    git push -u dest master
    

    Now /home/myproject is updated on the distant machine!

Note: this requires git version >= 2.4. If you don't have this one, and not available in your current distribution this can help: add-apt-repository ppa:git-core/ppa; apt update; apt install git.

Basj
  • 29,668
  • 65
  • 241
  • 451