Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save tworiverssolutions/454ea776290822b6d46b to your computer and use it in GitHub Desktop.

Select an option

Save tworiverssolutions/454ea776290822b6d46b to your computer and use it in GitHub Desktop.
Setting up Git to push from your workstation to a Live Web Server
# ------------------------------ On your local machiene ------------------------------------------------ #
# Set up a local repository on your computer. I use git hub to do this but you can also use the following
$ mkdir website && cd website
# Initialize the git repository on your site
$ git init
# Add your site files - (Lets say you add index to your site)
# You can either just add the file it self by doing this:
$ git add index.html
# OR
# You can add all the new changes by doing this:
$ git add .
# Next we commit our changes with the following including a comment;
$ git commit -q -m "The humble beginnings of my web site."
# ------------------------------ On Web Server ------------------------------------------------ #
# Ok now we set up our Remove repository on our web server.
# This repository is NOT supposed to be in the same folder as your website
# We use this a repository to push files to from your local computer and then we
# set up a hook to push those files to your web folder
# So lets set up a repository. You can put yours whereever you want I would suggest /var
$ sudo mkdir website.git && cd website.git
# Now we create a local repository (The one we will push to)
$ git init -bare
# Change direcotory to your git file then into the hooks file
$ cd website.git/hooks
# create a new file called post-recieve (in the hooks file)
$ nano post-recieve
# Add the following putting in the post-recieve file with the location of your live website files (Not git file location)
# This tells the repository where to push the files to when you commit them from your workstation
#---- Start Copy Below this line ---------------------#
#!/bin/sh
GIT_WORK_TREE=/var/www/www.example.org git checkout -f
#---- End Copy Above this line -----------------------#
# After saving the file we need to change the permission of the file
$ chmod +x hooks/post-receive
# ------------------------------ On your local machiene ------------------------------------------------ #
# Now we set up the remote mirror on your workstation
# Change direcotry back into your repository then tell git about your new webserver
# The ssh link is refering your git file
$ git remote add web ssh://server.example.org/var/website.git
# We now give our webserver a name so git knows what to call it when we push to it in the future
# I called my server "live"
$ git push live +master:refs/heads/master
# YEAA were done! Now in the future all you have to do to push to your server is
# Add new files
$ git add .
# Commit your changes
$ git commit -m "My updates yo"
# Push the changes
$ git push live
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment