Setup Git on remote and local server

1087 0 0 0

Last Updated : 2024-04-25 16:16:32

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 "your_email@example.com" 
//This will generate two files id_rsa -> the private Key, and id_rsa.pub -> 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/id_rsa.pub arabq@arab.com -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/id_rsa.pub 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 :


#!/bin/sh
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

NOW SETUP LOCAL MACHINE 


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


git remote add live ssh://server1.example.com/home/user/mywebsite.git

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


git remote add live 
ssh://user@server1.example.com/home/user/mywebsite.git

//In my case
git remote add dev ssh://arabq@arab.com/home/arabqlli/Git/myblog.git
git remote add dev ssh://arabq@arab.com:21098/home/arabqlli/Git/myblog.git

//Add the GitHub
git remote add GitHub https://github.com/username/repoName.git

IF AWS EC2


ADD AWS :


git remote add dev ssh://ec2-user@ec2-3-133-152-11.us-east-2.compute.amazonaws.com/home/ec2-user/Git/app.git 
ssh -vvv -i ~/.ssh/OhioKey.pem ec2-user@ec2-3-17-140-52.us-east-2.compute.amazonaws.com 'cat >> ~/.ssh/authorized_keys' < ~/.ssh/id_rsa.pub

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


EXTRA INFO 


** REMOVE REMOTE REPO : 


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 .