Your local repository content can be “pushed” (uploaded) on a remote server, an external storage (SSD, flash) or another partition or folder of your hard drive.
Let’s see all the steps for creating a remote repository on a shared hosting account in HostGator for a project named my_test. Git for Windows will be used in this example.
Create the local repository
# create the project folder
mkdir my_test
cd my_test/
git init
git status
# create an empty file and add it to repo
touch my_file.txt
git add .
git status
git commit -m "first commit message"
# see commit info
git log --stat
# get list of all branches, current one should be master
git branch
Generate SSH Public and Private RSA Keys on Hostgator server
Navigate: HostGator cPanel -> Security -> SSH Access -> Manage SSH Keys -> Generate a New Key
and create a public Key with Name mytest and Type: RSA (encryption algorithm)
Authorize the Public Key
Go to Manage -> Authorize
in order to activate the mytest key.
The public key (mytest.pub) and private key (mytest) files are located in .ssh folder on your server. You can see it with cPanel File Manager on server or from your terminal using a SSH connection or with a SSH client like Putty, SecureCRT, WinSCP, KiTTY.
Let’s see how to make a SSH connection from your terminal using the cpanel credentials:
ssh -p 2222 user@domain.com
# check the current path, it should be the root
pwd
cd .ssh/
ls
# authorized_keys mytest mytest.pub hola hola.pub
# see the content of a file
head mytest.pub
Enable shell access
Go tohttps://portal.hostgator.com/hosting/shared/userID/dashboard> > Server Info & Settings
and click Enable shell access
Download the keys from the server
Shared and Reseller HostGator accounts can be accessed via SFTP usign SSH protocol on port 2222 while Dedicated and VPS ones on port 22
Make a SSH connection in FileZilla using:
File Protocol: SFTP
Host name: yourDomainName or IP
Port number: 2222
Cpanel username and pass
Copy the authorized_keys, mytest, mytest.pub files to your local .shh folder
Similar, from cmd, just copy the remote .ssh folder to your local one:
cp -r -P 2222 hostgatorUser@x.x.x.x:/home/hostgatorUser/.ssh /home/localUser/.ssh/
Update known_hosts local file
The local .shh folder needs to contain known_hosts which list of all SSH server host public keys.
ssh-keyscan -H server_ip >> ~/.ssh/known_hosts
# and/or
ssh-keyscan -H server-domain >> ~/.ssh/known_hosts
# read file content
head known_hosts
Create the remote repository
Connect to your server from terminal:
ssh -p 2222 user@domain.com
cd ~/public_html
mkdir my_test
cd my_test/
# setup an empty repository
git init --bare
Link the local and remote Git repositories
In Git, the term origin is referred to the remote repository where you want to publish your commits. The default remote repository is called origin, although you can work with several remotes having a different name at the same time.
Open terminal from folder my_test and execute:
git remote add origin user@domain.com:/serverUserName/public_html/my_test
# to remove or replace the existing path
git remote set-url origin user@domain.com:/serverUserName/new_path/my_test
# list the remote url
git remote -v
# see the last commit(s)
git log --stat
git push origin master
# now the local branch 'master' is remotely also
git branch -a
# setup git to push the code branch by branch
git config --global push.default simple
If you want to clone the remote repository in another local folder called “my_test_clone”:
mkdir my_test_clone
git clone user@domain.com:/serverUserName/public_html/my_test
cd my_test_clone
ls
There’s a “reversal” way of doing these settings, using the ssh-keygen tool. Basically the keys will be generated locally and then uploaded on server. I’ll make another article about it.
Enjoy the git “pushes” and “commits” 🙂
Is this a typo when you mention ” local .shh folder”? Or the folder name is not important?
Thanks for good explanation.
it has to be .ssh the folder name