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:
cd folder
Then always first take the latest from Github otherwise you will get conflicts. Always run this just in case:
git pull origin master
If you get the following error:
error: Your local changes to the following files would be overwritten by merge
Then instead do:
git stash
git pull origin master
git stash pop
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
Remove your last commit but keep files as is
(so you can pull without merge conflict)
git reset HEAD^
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]"
When coming 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:
ssh-add /root/.ssh/id_rsa
Use cat /root/.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:
git init
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:
./gitupdate.sh
Set the cron:
crontab -e
Add this line at the bottom:
* * * * * /var/www/gitupdate.sh
Automatic pushing
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.
Other posts
- My Recommendations for a Happy Life
- Budget 100 - an old school magic format
- My Favorite Board Games and How I Play Them
- Switching from Windows PC to Mac and why I switched back
- Creating The Space War - The Card Game of My Dreams
- 24 Characteristics That Geniuses Have in Common
- Setting up and Managing a MySQL Server
- Canasta - The Great Card Game
- Annual report number 13 + 14: My Success
- Selling my SEO business TodaysWeb
He is the Founder of DomainStats and N.nu. Read his full about page.