Home > other >  synchronise git repo between two ssh machines
synchronise git repo between two ssh machines

Time:12-14

I'm beginner using git so afer hours configuring ssh between host and a virtual machine

I try to use git to synchronise data (code) between:

So Basically on Host :

I have a folder named MYFIRSTPROJECT, inside this folder i do :

git init (only once)

then on VM side i do:

git clone  git ssh://[email protected]/z/Project/MYFIRSTPROJECT/.git

--> this works the content appears

so I try to modify the code of a file MYFIRSTPROJECT/exemple.java on VM side and then I do :

git add --all

git commit -m 'my first commit'

git push git ssh://[email protected]/z/Project/MYFIRSTPROJECT/.git

Nothing change on Host side. I'm probably missing something but I expect to the file on host machines got refresh and the change made getting applied.

Otherwise when i'm doing a change on Host side (after the git clone on VM side) and i go back to VM side and i do a:

git pull

on this way the changed are properly apply

Thanks for your help

CodePudding user response:

By default, the working tree is not modified by a push.

The best practice is to create locally a bare repository, and put in it a post-receive hook which can be configured to execute any command you need.
Like going to your actual local repository (the one you created) and doing a git pull (to update the working tree from the commits you pushed to the bare repository from your VM.

So locally:

cd /d Z:
cd Project
git clone --bare MYFIRSTPROJECT MYFIRSTPROJECT.git
cd MYFIRSTPROJECT
git remote add origin ..\MYFIRSTPROJECT.git

Create a Z:\Project\Project\MYFIRSTPROJECT.git\hooks\post-receive file (eol as LF, not CRLF) with:

cd /Z/Project/MYFIRSTPROJECT
git pull

And from your VM:

cd /path/to/MYFIRSTPROJECT
git remote set-url origin  git ssh://[email protected]/z/Project/MYFIRSTPROJECT.git
# not /.git, but MYFIRSTPROJECT.git, which by naming convention, references a bare repo
  • Related