For this tutorial, I’ll be using Windows 7, Git Bash, and the website I’m using will be hosted on Hostgator.com.
What exactly are we doing? This is kind of like using FileZilla or cPanel File Manager x1000. We are going to create a repository with git on our local machine (c/user/yourname/testrepo) and push the changes to a remote repository on github.com (you’ll have to have an account). The most important part will be pulling the changes that we pushed to github to our website.
Step 1: Creating the local environment
Create a folder called testrepo in your home folder on your computer (with Git Bash, you can use the command “mkdir ~/testrepo”).
Next, with Git Bash, enter the folder by typing cd ~/testrepo and create an index.html file. You can do so with the command touch index.html Optional: add some text to the index.html file with your text editor, or you can use vi which is included with Git Bash. When you’re in the testrepo folder with Git Bash, type vi index.html Next, type i to start inserting text. Type anything you want. Next, hit the esc button on your keyboard to stop inserting. To save and exit, type :wq Now, type initialize the folder by typing git init and git add . and git commit -m ‘first commit’
Step 2: Creating the middle man on github
Next, create a repository on github called testrepo. After you do that, go back to Git Bash and type git remote add origin firstname.lastname@example.org:Your-Username/testrepo.git After that, type git push -u origin master After typing that command, you may or may not get an error like this “Error: Permission to user/repo denied to user/other-repo.” This is the error I got. According to this documentation on github, it has something to do with deploy keys, but I never created any deploy keys. My solution: create a private/public key pair. You’ll want to follow this tutorial on github. Warning: it does get technical. For step 3, I had to use the command eval $(ssh-agent -s) If you have no idea what you’re doing, ignore step 1, and move on to step 2. In step 3, I believe you’re telling your local Git Bash which key to use when trying to connect to your repo on github. In step 4.4, for the Title of the key, I would put the name of your computer, since your computer is the machine trying to access the repo on github. Okay, I deleted my keys that way I would have to go through all the steps, again, and, now, try running git push -u origin master again. For me, it, now, successfully pushes my local repo to the repo on github without asking for a password.
Step 3: Pulling Changes to Our Server via SSH
This is where it gets tricky and where the tutorial I was following will probably fail you. Right now, running the command ssh -l yourcpanelusername -p 2222 yourwebsite.com works for me. 2222 is the port that your hosting provider uses. My shared hosting provider is hostgator. However, if the above command does not work for you, you may have to create a new another pair of SSH keys. Hopefully, though, the above command works for you and you’re able to SSH into your server where your website it stored. Now, you are going to clone the repo from github to your public_html folder. In Git Bash, type cd ~/public_html This will take you to the public_html folder. Next, type git clone email@example.com:Your-User-Name/testrepo.git Again, it gets tricky here. I got the error “Permission denied (publickey). fatal: The remote end hung up unexpectedly” My solution: create more keys. Go back this tutorial and follow all the steps except for step 1. It’s a little bit more tricky doing this on your server, but it’s possible. The tricky part is in Step 4 because clip < ~/thisfile does not work when you’re on the server. Git Bash turns into jailcell bash, and you’re given a new set of commands. To copy the contents of ~/.ssh/id_rsa.pub, you’ll have to go into cPanel, click File Manager, view the file, and copy its contents. After you copy those contents onto github (follow the tutorial from step 4.1), on your server in jailcell (the bash that hostgator uses) type cd ~/public_html and run the command git clone firstname.lastname@example.org:Your-User-Name/testrepo.git I no longer go the error, and it cloned successfully without asking for a password.
Congratulations! You’re done! Now, on your local repo, make a change to the index.html file, run git add * and git commit -m ‘message’ and git push This will push your changes to your github repo. Next, SSH into your server with Git Bash: ssh -l yourcpanelusername -p 2222 yourwebsite.com Go to your repo directory cd ~/public_html/testrepo and run git pull This should pull the changes that you pushed to Github. If you have any questions, don’t hesitate to ask!