Setup Git on remote and local server

48 0 0 0

Last Updated : 2020-10-24 12:19:24

Setup a git repository on your local and remote servers together, So that you can manage and upload your website directly from your local git repo to online repo.

1 - Connect to your server :

ssh user@hostname

2- Generate a id_rsa pblic key on your local machine .

3- Put your shared ssh key from your local macheine to the server: (identify the server to your localmachine)

 ssh-copy-id -i ~/.ssh/ -p 21098

4- Do step 3 with any number of development servers you want to connect to in any time.

5- Your live production server need to have a GIT repo that can write to any web accessible web folder. - now create a Bare repository , it should be setup some where outside of your web root... You decide which directory to create it in... In my case I decided to create a dir called 'Git' just up the folder of www, and inside it I will create a git repository for each project hosting on this hosting package eg, mywebgit.git , or myblog.get .. etc.

6- Create the Bare git repo now

mkdir myblog.git
cd myblog.git
git init --bare --shared

7- Now create a Git hook that will check out files any time your instruct it, so now add a Post-Receive Hook : create a file named post-receive in the hooks directory of the git repo :

cd hooks;
touch post-receive

8- Add GiT work tree in the post receive file :

vi post-receive ; 

add the following content in in the post receive file :

GIT_WORK_TREE=/home/arabq/public_html/Projects/myblog git checkout -f

Then save 

9- Give execute permission for this file :

chmod +x hooks/post-receive


10- On local add remote reposiroty pointing to the web server

git remote add live ssh://

or if your remote username is different from the one on your local machine then this will help :

git remote add live 

//In my case
git remote add dev ssh://
git remote add dev ssh://

//Add the GitHub
git remote add GitHub



git remote add dev ssh:// 
ssh -vvv -i ~/.ssh/OhioKey.pem 'cat >> ~/.ssh/authorized_keys' < ~/.ssh/

11- Now time to push to live server

git push live +master:refs/heads/master



git remote rm live

Mohammed Anwar

Mohammed Anwar

Experienced technical lead PHP, MySQL and Laravel Developer for 15+ years, with proven ability to develop and create high-quality and optimized web applications. Great ability to build and optimize database design, schema and queries. Versed programing trainer and instructor delivering web courses and helping others to get into the field in a timely manner. Fast and eager learner for new technologies .