Git with GitHub the Simple Way
This is what is working for me and may not be the best for you. This is very beginner level. I am using the Git Bash from here.
Starting a new repo
Create the repo in GitHub, include readme.
Create a team in Github and make sure you have access to the repo.
Create a new folder in local computer
Start the git bash and do the following:
cd newfolder git init git remote add origin [email protected]:organisationname/repo-name.git git pull origin master
Now all is set with the new repo.
Git commands I use
First go to the correct folder:
Then always first take the latest from Github otherwise you will get conflicts. Always run this just in case:
git pull origin master
Add your changes:
git add . -A
Make a commit with the changes and a message:
git commit -m 'Message what this commit is about'
Then push it to Github:
git push origin master
Yes, I actually only use those commands.
Making the server pull from GitHub automatically
More info about this section here.
Install git on the server using this or similar:
apt-get install git
Generate a new SSH key using the same email that you have in GitHub:
ssh-keygen -t rsa -C "[email protected]"
Enter file in which to save the key ... just press enter.
The passphrase can be empty I guess otherwise it will complicate things. An attacker anyway needs the file with the key on the server and if they have access to the server, well then git is not something you really worry about. But in that case delete the key from Github.
Run this to ensure ssh-agent is enabled:
eval "$(ssh-agent -s)"
Add your generated SSH key to the ssh-agent:
cat /.ssh/id_rsa.pub and copy the SSH key to your clipboard.
Add your key to your Github account on this page. As a name write
nginx test server or whatever name you have for this server.
Test the connection according to this info.
Then create the folder on the server, such as:
cd /var/www/ mkdir foldername
Then initiate git on the folder using:
Automatic pulling from server using PHP
Create a new file such as gitupdate.php in GitHub with the following:
<?php $output = shell_exec('/usr/bin/git --work-tree=/var/www/foldername --git-dir=/var/www/foldername/.git pull [email protected]:name/repo-name.git'); echo "<pre>".$output."</pre>"; ?>
Then run this command on the server to get the file from Github to the server:
cd /var/www/foldername git --work-tree=/var/www/foldername --git-dir=/var/www/foldername/.git pull [email protected]:name/repo-name.git
Then make a change in for example the readme file in Github and enter gitupdate.php in your browser. Check if the readme file has been changed on the server.
If that works then just go to gitupdate.php in your browser whenever you want the files pulled to the server. No more FTP needed.
If it does not work it is probably due to a permission error. Don't waste 3 hours like me trying to solve that and just use cron instead according to the below instructions:
Automatic pulling from server using cron
We are making a shell script:
cd /var/www/ nano gitupdate.sh
Add the following to the file:
#!/bin/sh cd /var/www/directory /usr/bin/git --work-tree=/var/www/directory --git-dir=/var/www/directory/.git pull [email protected]:name/repo-name.git
Make it executable:
chmod +x gitupdate.sh
Test to run it and see the output:
Set the cron:
Add this line at the bottom:
* * * * * /var/www/gitupdate.sh
Just change the git commands, for example:
/usr/bin/git add . -A;/usr/bin/git commit -m "From server";/usr/bin/git push origin master
That's it for now.
Comments powered by Disqus
- Annual report number 11 + 12: My Success
- Annual report number 9 + 10: My Success
- Annual report number 8: My Success
- Guide: WordPress with Maximum Performance and Speed
- How to load WordPress in a few milliseconds using Redis
- Maximize the Link Juice of your Infographic with the use of the Canonical HTTP Header
- Why I recommend CloudFlare and why you should try it too
- Annual report number 7: My Success
- Sixth Annual Report: My Success