How to Use "git" on Your Own Machines to Manage Web Server (or Any Other) Files, Without Using Github or Any 3rd Party or Cloud Services

by cndg in Teachers > University+

142 Views, 1 Favorites, 0 Comments

How to Use "git" on Your Own Machines to Manage Web Server (or Any Other) Files, Without Using Github or Any 3rd Party or Cloud Services

pic_2023-10-25_18.13.41_228.png
xkcd_git.png

Nobody understands git.

Follow these instructions, if you want to use it on your own equipment to manage files - such as (for example) a web server.

Supplies

You will need git installed (Install Instructions)

These instructions make most sense if you have 2 or more machines involved (e.g. a web-server, and your laptop)

TLDR; ? see the last step!

Overview

Git stores all your files and everything else about them (history etc) inside a blob of file-system folders with random names. You're used to seeing all these inside a ".git" folder usually. I'll call all that the "gitblob", because if you look through that folder, you will never find your actual files in there - their names etc are all mangled.

To use git yourself (without 3rd party stuff or github etc), you have to decide one place (a folder on some specific machine) that will contain the gitblob of the latest and best copy of all your stuff. This gets called the "remote" (even if it's not remote). Note that your files will not be visible in here (they're stored as mangled names).

These are the people and things in this tutorial

  1. Chris - a developer (works on his laptop, and also sometimes on the Apache web server itself)
  2. Leo - a different developer (works on his laptop)
  3. Apache-server - a web server
  4. MyProject - this is the name of what we are working on, and inside here will be "index.html" which is the homepage of our website.

In this tutorial, we are going to:

  1. Create a new empty "remote" place named "MyProject" for the gitblob where all your latest files will be stored. This must be on the Apache-server
  2. "Clone" the above "MyProject" to one or more other places, which is where we will work on real files. This will live in a folder, which doesn't necessarily need to be named MyProject (but to avoid confusion - better if it is!)
  3. Set it all up so that every time anyone makes some kind of change - it will *automatically* extract all the right files and changes etc from the gitblob into a real folder with real files in it (e.g. /var/www/public_html )
  4. Note that whatever folder you want to use for #3 needs to be on the same machine as #1.
  5. Note also that everyone who has permission to change files in this project, will need to have read and write permissions to both the MyProject folder (#1 above) and the web folder (#3 above) on the Apache-server.

Decide and Make a Place for Keeping the Latest Copy of All Your Files

These instructions assume you're setting this up for a web server, so that 2 or more people can work on it - so - Apache-server is going to be your machine where you store the latest gitblob and where the auto-extraction will occur:

1. log in to your web_server


2. create a new empty folder somewhere that all the people who need to work on this have access to

mkdir -p /webwork/MyProject


3. adjust the folder so users in this project have write permissions:

chmod ugo+rw /webwork/MyProject


4. find out the name of your web server home folder, and make sure users in this project also have write permissions into that. It might be /var/www/html for example. This will be the auto-extract folder - files in here will get overwritten every time you do a "git push"

grep ^DocumentRoot /etc/httpd/conf/httpd.conf
ls -lsa /var/www/html
# check permissions on the above output, and adjust for your users accordingly


Create the Bare Git

go into your gitblob folder and run git as follows:-

cd /webwork/MyProject;
git init --bare


Decide and Make a Place for Working on Your Files

for this example, we will use "MyProject" in our local home folder on the Apache-server itself (e.g. /Users/chris/MyProject) but you don't have to name it "MyProject" if you don't want to, and it doesn't need to be on the web server either if you don't want

cd ~/ 
git clone /webwork/MyProject


if you do not want to edit files on your web server directly, but only (for example) on your laptop, you can, instead of the above, do this (on your laptop):-

cd ~/
git clone ssh://apache-server.example.com:22/webwork/MyProject

replacing apache-server.example.com with your server, and :22 with your SSH port number.

Set Up the Auto-extraction Solution

make the gitblob repo perform the auto-extractions:

cd /webwork/MyProject/hooks/;cat >post-update <<\EOF
#!/bin/bash
printf "post-update: running...\n"
env -u GIT_DIR git -C /var/www/html pull || exit
printf "post-update: ran ok\n"
EOF
chmod -c +x post-update


prepare the web folder for receiving the auto-extractions:

cd /var/www/html
get clone /webwork/MyProject
mv MyProject/.git .
rmdir MyProject


remember to check in your web server that accessing the .git folder is denied (e.g. restart your web server and visit http[s]://myproject.example.com/.git in your browser and make sure you're properly blocked)

Add Your First File, and Test

create a file, add and push it, and reload your web server to see it working:

cd ~/MyProject
echo hello > index.html
git add index.html
git commit -m "initial commit"
ls -lsa /var/www/html
git push
ls -lsa /var/www/html

Add a Second Place for Other People to Work on These Same Files Too

On some other machine (e.g. Leo's laptop)

cd ~/
git clone ssh://apache-server.example.com:22/webwork/MyProject

replacing apache-server.example.com with your server, and :22 with your SSH port number.

create a file, add and push it, and reload your web server to see it working:

cd ~/MyProject
echo hello2 > index.html
git add index.html
git commit -m "another commit"
git push


TLDR;

Alternatively, ignore everything above, and do this:-

Get my program which automates everything for you:

git clone https://github.com/gitcnd/git-meta.git


put the script somewhere convenient to use:

sudo ln -s `pwd`/git-meta/git-meta.pl /usr/local/bin/


run it (caution: adjust $master_location accordingly first, or leave the default as ~/Downloads/gitblobs/ )

git-meta.pl -newgit MyProject /var/www/html


you can then do this if you want to work on your web files from anyplace else (e.g. your laptop):-

git clone ssh://apache-server.example.com:22/home/leo/gitblobs/MyProject