Setup Git on remote and local server

598 0 0 0

Last Updated : 2022-07-02 19:26:50

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 local server :

ssh user@hostname

2- Generate a id_rsa pblic key on your local machine (Using puttyGen)  or using the following command on CMD, when you are in the .ssh directory.

ssh-keygen -t rsa -b 4096 -C "" 
//This will generate two files id_rsa -> the private Key, and -> the public key

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
//If the previous step didnot go well. Please know that the purpose is to add the public key, found in the local .ssh/ contents to the host/remote server authorized_keys file found in /home/ec2-user/.ssh directory. So you can do this with any text edit on the server like
sudo nano authorized_keys
//and add the contents to the end of file on a new line

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

//From certain local branch to remote master branch
git push <remote> <local branch name>:<remote branch to push into>
git push delta delta-start: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 .