I'm new to git and GitHub however have set up a local repository that updates the GitHub repository when I type:
[username@yourhostname Program] git add .
[username@yourhostname Program] git status
[username@yourhostname Program] git commit -m "a message"
[username@yourhostname Program] git push -u origin master
The next thing I want to be able to do is make it so that when I make changes and push them, these are also reflected in a directory on a live site.
I have installed git on the live server and set up a directory there called 'testdir'
.
I am tempted to just type 'git init'
when in this directory but am also seeing references on the web to 'git init --bare'
and getting a bit confused.
I'd appreciate it if anyone could provide a step by step list of commands to enable me to push changes made in a local repository to both GitHub and a live server.
These were my steps and demonstrate:
The following assumes that you:
'Program_One'
This process includes 8 steps:
‘testdir’
at 'yoursite.com/testdir'
'.git'
'.git/hooks'
and chmod its permissionsOpen your terminal and enter the following to ssh into your live server:
ssh [email protected] # the password will be your root password
Ask your host the preferred way to install git on your server and do that
Create directories, bare repo and post-receive file
[root@host /home/username/public_html] mkdir testdir
[root@host /home/username/public_html] cd testdir
[root@host /home/username/public_html/testdir] mkdir .git
[root@host /home/username/public_html/testdir] cd .git
[root@host /home/username/public_html/testdir/.git] git init --bare
[root@host /home/username/public_html/testdir/.git] cd hooks
[root@host /home/username/public_html/testdir/.git/hooks] vi post-receive
# press 'i', paste the following 2 lines, replacing with your details
#!/bin/sh
GIT_WORK_TREE=/home/username/public_html/livetest git checkout -f
# press 'esc', type :w, press enter, type shift+zz
[root@host /home/username/public_html/testdir/.git/hooks] chmod +x post-receive
[root@host /home/username/public_html/testdir/.git/hooks] exit
in the terminal, in your local repository, add your live server as a remote with:
[username@yourhostname Program_One] # make sure you are in your local repository
[username@yourhostname Program_One] git remote add my_great_remote [email protected]:/home/username/public_html/livetest/.git
# change ‘my_great_remote’ to the name you want to call your remote, taking note that the github remote is called ‘origin’.
add a text file called ‘my_text_file.txt’ to your local repository and then type the following in the terminal:
[username@yourhostname Program_One] # make sure you are in your local repository
[username@yourhostname Program_One] git add -all
[username@yourhostname Program_One] git status
[username@yourhostname Program_One] git commit -m "added text file"
[username@yourhostname Program_One] git push -u origin master
[username@yourhostname Program_One] git push -u my_great_remote master
the post-receive file will then copy files in your local repository to the ‘testdir’ directory allowing you to access the text file at:
mysite.com/testdir/my_text_file.txt
You would need to set up two remotes in your git configuration, one for github (this is probably your "origin" right now), and one for your other server. Push to github as usual, and then push to your other server with git push yourserver
.
If I understand you correctly, you want the pushed changes to be published somewhere (whether or not this is a good idea I will not debate) - and this can be accomplished by adding a "hook" script which is run after each push has been made to the git repository on the server. The hook could for instance copy some files to your www/ directory.
For information regarding adding remotes (github and your server) see man git-remote
For information regarding adding hooks, see man githooks
- I think the post-receive hook will suit you well here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With