Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to push to both GitHub and live server from local repository?

Tags:

git

github

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.

like image 997
user1063287 Avatar asked Jul 26 '13 08:07

user1063287


2 Answers

These were my steps and demonstrate:

  • Adding a text file to a local repository
  • Pushing these changes (ie the new file) to both GitHub and a live server

The following assumes that you:

  • have already set up a remote at GitHub and can push changes there
  • Are working in a directory in your local repository called 'Program_One'

This process includes 8 steps:

  • Check if you have ssh access to your live server
  • Install git on your live server
  • Create the directory ‘testdir’ at 'yoursite.com/testdir'
  • Create a directory in that directory called '.git'
  • Create a ‘bare repo’ in that directory
  • Create a post-receive file in '.git/hooks' and chmod its permissions
  • Add a live server as a remote
  • Push to the live server and GitHub

Open 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
like image 170
user1063287 Avatar answered Nov 15 '22 01:11

user1063287


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.

like image 34
Jonatan Avatar answered Nov 15 '22 01:11

Jonatan